我想根据几个参数来计算交货日期:
例如,我们是1月28日,星期一。我的承运人的最短日期为3天和5天,因此我的送货时间应为1月31日星期四至2月4日,因为附加的限制是该承运人不送货周六和周日。我正在寻找一个计算该日期的PHP函数,但是我有承运人在星期六预定,因此,如果承运人在星期六交货,则交货时间为1月31日至2月2日。这是我拥有的一些变量:
$minDays = (int)$carrierDefault->delay_min;
$maxDays = (int)$carrierDefault->delay_max;
$saturdayDelivery = $carrierDefault->delivered_saturday;
谢谢您的帮助。
答案 0 :(得分:0)
这对您有帮助吗?
<?php
function isWeekend($date , $ignore_saturday=false) {
$weekDay = date('w', strtotime($date));
return ($weekDay == 0 || ($weekDay == 6 && !$ignore_saturday) ) ;
}
function selectDeliveryinterval($startDate, $min_delivery = 3, $max_delivery = 5 , $ignore_saturday=false) {
$isfull = true;
$counter = 0;
$dates = array();
while($isfull) {
$startDate->modify('+1 day');
if(!isWeekend($startDate->format('Y-m-d'),$ignore_saturday)) {
$dates[] = clone $startDate;
$counter++;
}
if($counter == $max_delivery) {
$isfull = false;
}
}
// the array contain the dates in the delivery interval
return [$dates[$min_delivery - 1], $dates[$max_delivery - 1]];
}
$date = new DateTime('2019-01-28');
$res = selectDeliveryinterval($date,3,5,true);
echo 'your delivery date min is '.$res[0]->format('Y-m-d');
echo 'your delivery date max is '.$res[1]->format('Y-m-d');
输出:
您的交货日期下限是2019-02-20
最长交货日期是2019-02-22
如果承运人在星期六工作:
您的交货日期下限为2019-01-31
最长交货日期是2019-02-02