如何在给定工作日列表的情况下找到最早的即将到来的日期?

发布于 2024-12-04 02:42:29 字数 252 浏览 1 评论 0原文

PHP 中的日期对我来说是一场噩梦,所以请帮助我的其他编码人员...我想通知客户他们的订单将在哪一天交付。它的工作原理如下:

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

我如何计算下一个可用交货日期并通知客户?

谢谢。

Dates in PHP are a nightmare for me so please help me out fellow coders... I want to notify customers about the day their order will be delivered. It works like this:

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.

How can I calculate the NEXT AVAILABLE delivery date and notify the customer?

Thanks.

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

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

发布评论

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

评论(3

不知所踪 2024-12-11 02:42:29

这当然不是最快或最聪明的答案,但阅读代码将是一种乐趣。

假设我们在 A 区发货:

$dates = array(
    new DateTime('next monday'), // magic!
    new DateTime('next wednesday'),
    new DateTime('next friday'),
);

// Seems to work because since PHP 5.2.2 DateTime objects
// can be compared with the < and > operators    
$shippingDate = min($dates);

echo $shippingDate->format('Y-m-d');

您可能需要查看 PHP 中提供的相对日期格式,这就是“下周一”魔法发生的部分。有关如何使用 $shippingDate 的信息,请参阅类 日期时间

更新

为了完整起见,这里是一个更老式的版本,不需要 PHP 5.3 并且也应该更快(尽管速度实际上与这里无关)。我不太喜欢它,因为验证它是否正常工作并不容易。与上面的版本相比,这个版本在我第一次编写时有一个错误。简单就是好的。

$today = date('w');

// These values are from http://www.php.net/manual/en/function.date.php
$shippingDays = array(
    1, // mon
    3, // wed
    5, // fri
);

// Each of these weekdays is how many days into the future?
foreach($shippingDays as &$day) {
    $day = (7 + $day - $today) % 7;
}

// Get the earliest one, but not if it's today
// array_filter is used to remove a possible 0
$daysInFuture = min(array_filter($shippingDays));
$shippingDate = new DateTime('+'.$daysInFuture.' days');
echo $shippingDate->format('Y-m-d');

查看实际操作

This will certainly not be the fastest or most clever answer, but it's going to be a pleasure to read the code.

Assuming we are shipping in zone A:

$dates = array(
    new DateTime('next monday'), // magic!
    new DateTime('next wednesday'),
    new DateTime('next friday'),
);

// Seems to work because since PHP 5.2.2 DateTime objects
// can be compared with the < and > operators    
$shippingDate = min($dates);

echo $shippingDate->format('Y-m-d');

You might want to take a look at the relative date formats available in PHP, this is the part where the "next monday" magic happens. For information on what you can do with $shippingDate, see the documentation on class DateTime.

Update

For completeness, here is a more old-school version which does not need PHP 5.3 and should also be faster (although speed is practically irrelevant here). I don't like it as much, because it's not easy to verify that it works correctly. In contrast to the version above, this one had a bug when I first wrote it. Simple is good.

$today = date('w');

// These values are from http://www.php.net/manual/en/function.date.php
$shippingDays = array(
    1, // mon
    3, // wed
    5, // fri
);

// Each of these weekdays is how many days into the future?
foreach($shippingDays as &$day) {
    $day = (7 + $day - $today) % 7;
}

// Get the earliest one, but not if it's today
// array_filter is used to remove a possible 0
$daysInFuture = min(array_filter($shippingDays));
$shippingDate = new DateTime('+'.$daysInFuture.' days');
echo $shippingDate->format('Y-m-d');

See it in action.

给妤﹃绝世温柔 2024-12-11 02:42:29

试试这个:

// Info
$date = array(date("d"), date("m"), date("Y"));
$zone = "A";
// ------

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

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

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

// Retrieve last day from the zone
end($zones[$zone]);
$last_day = key($zones[$zone]);

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

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

echo "Next date: " . date("d/m/Y - l", $mk);

Try this:

// Info
$date = array(date("d"), date("m"), date("Y"));
$zone = "A";
// ------

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

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

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

// Retrieve last day from the zone
end($zones[$zone]);
$last_day = key($zones[$zone]);

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

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

echo "Next date: " . date("d/m/Y - l", $mk);
暮色兮凉城 2024-12-11 02:42:29
$timestamp = strtotime('next Monday');
$date      = date('Y-m-d', $timestamp);
$timestamp = strtotime('next Monday');
$date      = date('Y-m-d', $timestamp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文