Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add two business days to a date array excluding holiday dates

Tags:

date

php

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.

like image 389
charlie Avatar asked Jul 24 '17 22:07

charlie


People also ask

How do you add days to a holiday excluding holidays in Excel?

Select a blank cell and type this formula =WORKDAY(A2,B2), and press Enter key to get result. Tip: In the formula, A2 is the start date, B2 is the days you want to add. Now the end date which add 45 business days excluding weekends has been shown.

How do you calculate working days between two dates in Excel excluding weekends and holidays?

The NETWORKDAYS function in Excel returns the number of workdays between two dates, excluding weekends and, optionally, the holidays you specify. The first two arguments are obligatory and the third one is optional: Start_date - initial date from which to start counting working days.


1 Answers

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);
like image 78
fubar Avatar answered Oct 04 '22 19:10

fubar