charlie charlie - 1 month ago 10
PHP Question

Add two business days to a date array excluding holiday dates

I am trying to add 2 weekdays to a date, but I also want to exclude the days in an array.

array of dates to exclude:

$bankHolidays = array();
foreach($obj as $e) {
if($e->division == 'england-and-wales') {
foreach($e->events as $events) {
$bankHolidays[] = $events->date;
}
}
}


Adding 2 working days to date

$ret = date('d-m-Y', strtotime($bill["charge_customer_at"]. ' +2 weekdays'));


How can I include the array of dates to exclude?

As an example, if my date was
2017-07-19
and i want to add 2 week days, that would output
2017-07-21
.

But if
2017-07-21
was in my array, it should skip this date and continue adding 2 weekdays so the output would end up being
2017-07-24
because of the weekend as well.

Answer Source

You could do something as simple as use a while loop.

$date = '2017-07-25';
$reserved = ['2017-07-27', '2017-07-28'];
$days = 2;

while ($days > 0) {
    $date = date('Y-m-d', strtotime($date . ' +1 weekday'));
    if (! in_array($date, $reserved)) $days--;
}

var_dump($date);