在 PHP 个验证假期中添加小时数

Adding hours in PHP validating holidays

我有一个 PHP 代码可以在我的系统中生成订单的交货日期和时间以验证时间表和星期日,这样:

<?php
$hours = 6;

$date = date('Y-m-d h:i A');
$delivery = date("d-m-Y h:i A", strtotime("+$hours hours", strtotime($fecha)));
echo $delivery;

    if (date('H') >= 18 || date('H')<9 || date('w')==0){
        $c=strtotime("tomorrow 09:00");
        $date = date("Y-m-d h:i A", $c);

        $delivery = date("d-m-Y h:i A", strtotime("+$hours hours", strtotime($fecha)));

        echo $delivery;
    }


?>

如果订单是在下午 6 点之后或上午 9 点之前下达的,或者如果当天是星期日,则订单将在第二天上午 9 点和递增的时间进行。但我想验证当天和第二天是否是星期日或假期,如 7 月 4 日(04-07)或圣诞节(25-12)以在它们的第二天(05-07 或 26-12)生成交货日期时间).

如何修改?

我需要一些帮助。

首先我会使用 DateTime 对象。作为下一步,您可以将验证部分与修改部分分开。 运行 循环验证。今天,明天等等。您可能需要一个临时日期对象,您可以在不丢失原始日期的情况下对其进行修改。

只需 运行 您的代码在一个 while 循环中,这样您就可以根据需要多次调用 tomorrow 09:00

$hours = 6;

$orderPickup = time(); // Normally order should be picked up right now
$delivery = strtotime("+$hours hours", $orderPickup);

while(
    date('H', $orderPickup) > 18 
    || date('H', $orderPickup) < 9 
    || date('w', $orderPickup) == 0 // No order pickup on sunday
    || date('m-d', $orderPickup) == '12-25' // No order pickup on Christmas-day
    || date('w', $delivery) == 0) // No delivery on sunday
    || date('m-d', $delivery) == '12-25' // No delivery on Christmas-day
) {
    // $orderPickup will be 9 am next day
    $orderPickup = strtotime('tomorrow 9:00:00', $orderPickup);
    $delivery = strtotime("+$hours hours", $orderPickup);

    // If tomorrow 9:00 is stil not suitable, the while-loop will run again
}
echo "Delivery on " . date("d-m-Y h:i A", $delivery);