如何将星期六指定为 strtotime 的工作日 [英] How to specify saturday as a week day for strtotime
本文介绍了如何将星期六指定为 strtotime 的工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在当前日期的基础上增加 2 个工作日.我打算用 strtotime 来做,但 strtotime 的工作日不包括星期六.
I need to add 2 business days to the current date. I was going to use strtotime for it, but strtotime's weekdays don't include saturday.
$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);
如果您在星期五运行它,它将输出星期二.但它实际上应该在星期一返回.
This would output Tuesday if you run it on Friday. But it actually should return Monday.
如何将星期六添加为工作日?
How can i add Saturday as a weekday?
推荐答案
从特定日期+偏移天数开始获取下一个工作日:
Get next business day from specific date + number of day offset:
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) );
这篇关于如何将星期六指定为 strtotime 的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文