Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top