我正在尝试创建一个交货时间估算器,该估算器基于今天的日期来显示接收物品需要多少天。
一切工作都很好。我要坚持的最后一部分是如何设置日期,以便每天太平洋时间下午2点将日期更改为第二天。
现在,我已将其设置为使用太平洋时间。只需要将更改日期更改为太平洋标准时间下午2点至第二天的更改日期即可。
我考虑过只是更改时区,但不确定采用夏令时是否会成为使用这种方法要考虑的问题。
我添加了下面正在使用的代码:
<?php
if ((strlen(ini_get('date.timezone')) < 1) && function_exists('date_default_timezone_set')) {
date_default_timezone_set('America/Los_Angeles');
}
function getNextMondayDate($date) {
$day = date('w', $date);
if ($day==0) {
$ret = strtotime("+1 day", $date);
} else if ($day==6) {
$ret = strtotime("+2 day", $date);
} else {
$ret = strtotime("now", $date);
}
return $ret;
}
function businessDays($days, $time) {
for ($i=0; $i<$days; $i++) {
$time = strtotime('+1 day', $time);
$day = date('w',$time);
if ($day==0) {
$time = strtotime('+1 day', $time);
} else if ($day==6) {
$time = strtotime('+2 day', $time);
}
}
return $time;
}
$date = date("l, F jS");
$dateStart = getNextMondayDate(strtotime("now"));
if (date('w')==6 || date('w')==0){
// If the order is placed on Saturday or Sunday...
// THe numbers you see below are measured in days. In the example below
// The delivery times for standard shipping are from 2-4 days from today
$dateStandardMinMG = date("l, F jS",getNextMondayDate(businessDays(2,$dateStart)));
$dateStandardMaxMG = date("l, F jS",getNextMondayDate(businessDays(4,$dateStart)));
$dateExpressMinMG = date("l, F jS",getNextMondayDate(businessDays(0,$dateStart)));
$dateExpressMaxMG = date("l, F jS",getNextMondayDate(businessDays(1,$dateStart)));
}else{
// Otherwise, use these estimates
$dateStandardMinMG = date("l, F jS",getNextMondayDate(businessDays(3,$dateStart)));
$dateStandardMaxMG = date("l, F jS",getNextMondayDate(businessDays(5,$dateStart)));
$dateExpressMinMG = date("l, F jS",getNextMondayDate(businessDays(1,$dateStart)));
$dateExpressMaxMG = date("l, F jS",getNextMondayDate(businessDays(2,$dateStart)));
}
?>
答案 0 :(得分:1)
如果我理解正确,如果输入时间超过2 PM,您想在计算中加上+1天吗?为了使它与您的方法保持一致,我将为日期添加一个日期格式,并像在工作日中一样使用它
自从businessDays()计算出交货时间以来,我认为这很合适
function businessDays($days, $time) {
// Past 2 PM check should be outside the for loop
$hours = date('G', $time);
$weekend = date('w',$time);
if ($hours > 14 AND $weekend != 0 AND $weekend !=6) {
$time = strtotime('+1 day', $time);
}
for ($i=0; $i<$days; $i++) {
$time = strtotime('+1 day', $time);
$day = date('w',$time);
if ($day==0) {
$time = strtotime('+1 day', $time);
} else if ($day==6) {
$time = strtotime('+2 day', $time);
}
}
return $time;
}
答案 1 :(得分:1)
因此,您正在寻找一个函数来返回距当前日期一定天数的下一个工作日–除非是在周末或下午2点之后,否则您希望下一个工作日是一定的天数从下一个工作日开始的几天?这应该做到这一点。我把它们都铺满了,所以应该很容易理解。
function getDeliveryDay(int $days, int $date = null):int {
if ($date === null) {
$date = time();
}
$base = "now";
if (date("H", $date) >= 14) {
// if it's late, calculate from tomorrow
$days++;
}
if (in_array(date("w", $date), [0, 6])) {
// if it's a weekend, calculate from monday
$base = "next monday";
}
for ($i = 0; $i <= $days; $i++) {
// make sure delivery day isn't Saturday or Sunday
// don't count those days in processing time either
$result = strtotime("$base +$i days", $date);
$w = date("w", $result);
if (in_array($w, [0, 6])) $days++;
}
return $result;
}
测试(星期四3:00):
echo date("l, F jS", getDeliveryDay(0)) . "\n"; // should be Friday
echo date("l, F jS", getDeliveryDay(1)) . "\n"; // should be Monday
echo date("l, F jS", getDeliveryDay(2)) . "\n"; // should be Tuesday
echo date("l, F jS", getDeliveryDay(3)) . "\n"; // should be Wednesday
echo date("l, F jS", getDeliveryDay(4)) . "\n"; // should be Thursday
echo date("l, F jS", getDeliveryDay(5)) . "\n"; // should be Friday
输出:
Friday, April 5th
Monday, April 8th
Tuesday, April 9th
Wednesday, April 10th
Thursday, April 11th
Friday, April 12th
使用您的原始代码:
$dateStandardMinMG = date("l, F jS", getDeliveryDay(2));
$dateStandardMaxMG = date("l, F jS", getDeliveryDay(4));
$dateExpressMinMG = date("l, F jS", getDeliveryDay(0));
$dateExpressMaxMG = date("l, F jS", getDeliveryDay(1));