PHP如何在这个例子中获得下一个日期?
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我就是这样做的。
注意
我没有花太多时间来找出处理所有业务规则的最佳方法,但我的所有测试似乎都有效。
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.