php - 使用时间截止计算“下一个工作日”
问题描述
我正在尝试创建一个交货时间估算器,根据今天的日期,它将显示收到物品需要多少天。
一切都很好。我坚持的最后一部分是如何设置日期,以便太平洋时间每天下午 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)));
}
?>
解决方案
如果我理解正确,如果输入时间超过下午 2 点,您想在计算中添加 +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;
}
推荐阅读
- reactjs - Material UI 对话框组件 - 如何显示它?
- odata - OData - 一对多过滤所有没有数据的记录
- javascript - 调用 page.waitForTimeout 后,Puppeteer 无法单击元素
- python - 添加基于 pytest 的框架作为依赖项
- node.js - 如何使用node js ad mongodb制作一个针对多个国家的分类广告网站?
- python - Websocket 数据流索引
- java - Java Saxon 10.5 XSL 3 转换,未找到 href 文件
- javascript - 如何使用 Next.js 将 PNG 徽标添加到 Tailwind CSS 导航栏?
- python - 如何优化以下python函数以更快地执行?
- c# - C# ASP.NET MVC:动态更新视图