PHP如何在这个例子中获得下一个日期?

发布于 2024-12-11 06:48:34 字数 1903 浏览 0 评论 0原文

我有 2 个运输区域,A 和 A。 B. A区订单每周一、三、四发货。星期五,而 B 区是星期二、星期四、星期六。对于每个订单,交货日安排在下一个可用日,具体取决于区域。请注意,如果有人在周一下订单,则货物将在下一个可用日期交付,即 B 区为周二,A 区为周三。

但是:如果订单晚于 18:00 (下午 6 点),而客户所在的区域位于明天的送货区域,那么我们必须将送货日期提前到该区域的下一个可用日期(因为订单尚未准备好) ,通知时间太短)。

这是代码,它工作正常,除了最后一部分我需要检查时间、获取常规交货日期并将其与明天的日期进行比较。

<? 
$date = array(date("d"), date("m"), date("Y"));

$zones = array("A" => array(1 => "Monday",    
                            3 => "Wednesday", 
                            5 => "Friday")     

              ,"B" => array(2 => "Tuesday",    
                            4 => "Thursday",   
                            6 => "Saturday")); 

$found = false;
$days_plus = 1; // always begin from next day

// Retrieve last day from the zone
end($zones[$zone]); //Friday or Saturday
$last_day = key($zones[$zone]); //5 or 6

do {
    $mk = mktime(0, 0, 0, $date[1], ($date[0] + $days_plus), $date[2]);
    $week = date("w", $mk); // current day of week 

    if ($week <= $last_day) // if weekday not passed last day of zone
    {
        if (!isset($zones[$zone][$week]))
        {
            $days_plus++;
        }
        else
        {
            $found = true;
            $day = $last_day;
        }
    }
    else
    {
        $days_plus++;
    }
} while (!$found);

$deliverydate = date("d/m/Y", $mk); // regular delivery date
$tomorrow = date('d/m/Y', strtotime('tomorrow')); 

//Now, check if order is placed after 6pm 
if ((date("d/m/Y", $mk)==$tomorrow) && (date('G') > 18)) {
    // HERE'S my problem, how do I advance the
    //delivery date to next day in the same zone?
}
echo $deliverydate; 
?>

注意:我尝试将循环转换为函数 findNextDay($days_plus, $date, $last_day, $zone) ,以便我可以使用不同的 $days_plus 值(增加 x 天)再次调用它,但我无法得到它去工作。对于任何感兴趣的人这里是修改后的版本

I have 2 shipping zones, A & B. Orders for zone A are delivered each Monday, Wednesday & Friday, whereas zone B is on Tuesday, Thursday, Saturday. For each order, the delivery day is scheduled for the NEXT AVAILABLE day, depending on the zone. Please consider that if someone places an order on Monday the goods will be delivered on the NEXT available date, that would be Tuesday for zone B and Wednesday for zone A.

BUT: If an order is placed later than 18:00 (6pm) and the customer's area is within the tomorrow's delivery zone, then we have to advance the delivery date to the next available day for that zone (because the order won't be ready yet, too short notice).

Here's the code and it works fine except for the last part where I need to check time, get the regular delivery date and compare it with tomorrow's date.

<? 
$date = array(date("d"), date("m"), date("Y"));

$zones = array("A" => array(1 => "Monday",    
                            3 => "Wednesday", 
                            5 => "Friday")     

              ,"B" => array(2 => "Tuesday",    
                            4 => "Thursday",   
                            6 => "Saturday")); 

$found = false;
$days_plus = 1; // always begin from next day

// Retrieve last day from the zone
end($zones[$zone]); //Friday or Saturday
$last_day = key($zones[$zone]); //5 or 6

do {
    $mk = mktime(0, 0, 0, $date[1], ($date[0] + $days_plus), $date[2]);
    $week = date("w", $mk); // current day of week 

    if ($week <= $last_day) // if weekday not passed last day of zone
    {
        if (!isset($zones[$zone][$week]))
        {
            $days_plus++;
        }
        else
        {
            $found = true;
            $day = $last_day;
        }
    }
    else
    {
        $days_plus++;
    }
} while (!$found);

$deliverydate = date("d/m/Y", $mk); // regular delivery date
$tomorrow = date('d/m/Y', strtotime('tomorrow')); 

//Now, check if order is placed after 6pm 
if ((date("d/m/Y", $mk)==$tomorrow) && (date('G') > 18)) {
    // HERE'S my problem, how do I advance the
    //delivery date to next day in the same zone?
}
echo $deliverydate; 
?>

Note: I tried converting the loop into a function findNextDay($days_plus, $date, $last_day, $zone) so that I could call it again with a different $days_plus value (increased by x days) but I couldn't get it to work. For anyone interested here is the modified version

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2024-12-18 06:48:34

我就是这样做的。

注意
我没有花太多时间来找出处理所有业务规则的最佳方法,但我的所有测试似乎都有效。

<?php


error_reporting(E_ALL);
ini_set('display_errors','On');

date_default_timezone_set('America/Los_Angeles');

$zones = array('A', 'B');
$zone_key = array_rand($zones);
$zone = $zones[$zone_key];


function getDeliveryDate($zone, $d = NULL, $time = NULL) {
    if ($time === NULL) 
        $time = date('G');
    if ($d === NULL)
        $now = strtotime('now');
    else
        $now = strtotime($d);
    $d = date('w', $now);
    $days = 1;
    if ($zone == 'A') {
        if ($d % 2) $days += 1;
        else if ($time > 18) $days += 2;

        if ($d + $days == 7) $days += 1;
        elseif ($d + $days > 7) $days -= 1;
    } else {
        if (! ($d % 2)) $days += 1;
        else if ($time > 18) $days += 2;
        if ($d + $days > 7) $days += 1;
    }

    $delivery_date = strtotime("+${days}days", $now);

    echo "For zone $zone, schedule on " . date('D', $now) . ", at $time, will deliver on " . date('m-d-Y', $delivery_date) . " which is a " . date('D', $delivery_date) . "\n";
}

$days = array('10/16/2011', '10/17/2011', '10/18/2011', '10/19/2011', '10/20/2011', '10/21/2011', '10/22/2011');
$times = array(17, 20);

foreach ($zones as $zone) {
    foreach ($days as $d) {
        foreach ($times as $t) {
            getDeliveryDate($zone, $d, $t);
        }
    }
}

This is how I would do it.

NOTE
I didn't give a whole lot of time to figuring out the best way to handle all of the business rules, but all of my tests seemed to work.

<?php


error_reporting(E_ALL);
ini_set('display_errors','On');

date_default_timezone_set('America/Los_Angeles');

$zones = array('A', 'B');
$zone_key = array_rand($zones);
$zone = $zones[$zone_key];


function getDeliveryDate($zone, $d = NULL, $time = NULL) {
    if ($time === NULL) 
        $time = date('G');
    if ($d === NULL)
        $now = strtotime('now');
    else
        $now = strtotime($d);
    $d = date('w', $now);
    $days = 1;
    if ($zone == 'A') {
        if ($d % 2) $days += 1;
        else if ($time > 18) $days += 2;

        if ($d + $days == 7) $days += 1;
        elseif ($d + $days > 7) $days -= 1;
    } else {
        if (! ($d % 2)) $days += 1;
        else if ($time > 18) $days += 2;
        if ($d + $days > 7) $days += 1;
    }

    $delivery_date = strtotime("+${days}days", $now);

    echo "For zone $zone, schedule on " . date('D', $now) . ", at $time, will deliver on " . date('m-d-Y', $delivery_date) . " which is a " . date('D', $delivery_date) . "\n";
}

$days = array('10/16/2011', '10/17/2011', '10/18/2011', '10/19/2011', '10/20/2011', '10/21/2011', '10/22/2011');
$times = array(17, 20);

foreach ($zones as $zone) {
    foreach ($days as $d) {
        foreach ($times as $t) {
            getDeliveryDate($zone, $d, $t);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文