我有2个运输区,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天)但我无法'让它工作。对于任何感兴趣的人here is the modified version
答案 0 :(得分:0)
我就是这样做的。
注意强>
我没有花很多时间来找出处理所有业务规则的最佳方法,但我的所有测试似乎都有效。
<?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);
}
}
}