将php日期和时间修改为办公时间内的星期天php



我有一个PHP函数,它可以修改当前日期时间并添加2周,同时向数据库添加一个条目,并在2周期限到期后向用户发送电子邮件。该函数将覆盖该条目,并为下一个用户保留该表条目。

public function UpdateDemos(){
global $wpdb;
$table_name = $wpdb->prefix . "yearbook_hub_accounts";
$results = $wpdb->get_results( "SELECT * FROM $table_name", OBJECT );
foreach ($results as $account){
if($account->requested){
$requestDate = new DateTime($account->requested);
$requestDateExpires = $requestDate->modify("+2 weeks");
$todaysDate = new DateTime();
if($requestDateExpires->getTimestamp() < $todaysDate->getTimestamp()){
self::send_notification_email($account);
$wpdb->update($table_name, ['requested' => null, 'email' => null, 'password' => self::generatePassword()], ['id' => $account->id]);
}
}
}

从外部来看,我们需要与第三方更新密码,而这方面的问题是由于功能设置为+2周,这些密码重置可能是白天或晚上的任何时间。

我真的希望这个功能在办公时间之间修改日期+2周,所以问题是我修改+2周的最佳途径是什么

还是调整

$requestDateExpires->getTimestamp() < $todaysDate->getTimestamp()

只在周一至周五重置9-5。

第一次尝试是使用类似开关案例的东西来获得正确的日间隔

$today = date("D");
switch($today){
case "Mon":
case "Tue":
case "Wed":
case "Thurs":
case "Fri":
$days = 14;
break;
case "Sat":
$days = 16;
break;
case "Sun":
$days = 15;
break;
default:
break;
}

我希望这就是你想要的。。。

<?php
// first identify your work days
// 1 = monday, 2 = tuesday, 7 = sunday
$office_days    = array(
1, //monday
2,
3,
4,
5
);
// set the office hours
$day_start      = "09:00:00";
$day_end        = "17:00:00";
// change the request date to test different scenarios
$request_date   = new dateTime("2021-08-14 18:32:10.000");
// use the N dateTime::format to identify if the request date is an work day
if(!in_array( $request_date->format('N'),$office_days)) { // we could go straight to the while loop, but you want set it to 9am on the next work day if it's outside normal work days
while(!in_array( $request_date->format('N'),$office_days)) {
$request_date->modify('+1 day');
}
$request_date->setTime(9,0,0); //hour,minute,second
}
echo "Request date: " . $request_date->format('Y-m-d H:i:s');

最新更新