How to specify saturday as a week day for strtotime

故事扮演 提交于 2021-02-10 08:32:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!