【问题标题】:Modify php date and time to week days during office hours PHP在办公时间修改php日期和时间到工作日PHP
【发布时间】:2021-07-14 13:28:16
【问题描述】:

我有一个 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;
}

【问题讨论】:

  • 具体规则是什么?当 +2 周是在周末或工作时间以外,您希望发生什么?另外,你有什么办法解决这个问题?
  • 规则是,如果在办公时间之外重置,则在下一个工作日上午 9 点重置
  • 这应该不难实现。你拿日期,确定星期几和时间。如果不在工作时间内,则将其移至下一个工作日 9 点。该流程的任何特定部分遇到问题?

标签: php date time


【解决方案1】:

我希望这就是你要找的……

<?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');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多