我需要在当前日期的基础上添加 2 个工作日。 我本来打算使用 strtotime,但 strtotime 的工作日不包括星期六。
$now = date("Y-m-d H:i:s");
$add = 2;
$format = "d.m.Y";
if(date('H') < 12) {
$add = 1;
}
$date = strtotime($now . ' +'.$add.' weekdays');
echo date($format, $date);
如果您在星期五运行它,则会输出星期二。但它实际上应该在周一返回。
如何将星期六添加为工作日?
最佳答案
从特定日期+偏移天数获取下一个工作日:
function get_next_business_date($from, $days) {
$workingDays = [1, 2, 3, 4, 5, 6]; # date format = N (1 = Monday, ...)
$holidayDays = ['*-12-25', '*-01-01', '2013-12-24']; # variable and fixed holidays
$from = new DateTime($from);
while ($days) {
$from->modify('+1 day');
if (!in_array($from->format('N'), $workingDays)) continue;
if (in_array($from->format('Y-m-d'), $holidayDays)) continue;
if (in_array($from->format('*-m-d'), $holidayDays)) continue;
$days--;
}
return $from->format('Y-m-d'); # or just return DateTime object
}
print_r( get_next_business_date('today', 2) );
关于php - 如何将周六指定为 strtotime 的工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21186962/