问题
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?
回答1:
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) );
demo
来源:https://stackoverflow.com/questions/21186962/how-to-specify-saturday-as-a-week-day-for-strtotime