Get the next 3 business dates, skip weekends and holidays

I am trying to get the next 3 days using php, but if the date is a holiday or a specific holiday, I would skip this date.

For example, if the date is Monday, December 23, 2013, my array('2013-12-24', '2013-12-25'); script will return my vacation dates

Monday, December 23, 2013
Thursday, December 26, 2013
Friday, December 27, 2013
Monday, December 30, 2013

This is my current code:

$arr_date = explode('-', '2013-12-23');
$counter = 0;
for ($iLoop = 0; $iLoop < 4; $iLoop++)
{
    $dayOfTheWeek = date("N", mktime(0, 0, 0, $arr_date[1], $arr_date[2]+$counter, $arr_date[0]));
    if ($dayOfTheWeek == 6) { $counter += 2; }
    $date = date("Y-m-d", mktime(0, 0, 0, $arr_date[1], $arr_date[2]+$counter, $arr_date[0]));
    echo $date;
    $counter++;
}

The problem I'm facing is I'm not sure how to go at all except for vacation dates.

+4
source share
2 answers

Use DateTimefor reprocessing over the next days and check the string matching check to see if the next generated day belongs to holiday or weekend arrays usingin_array

$holidays = array('12-24', '12-25');
$weekend = array('Sun','Sat');
$date = new DateTime('2013-12-23');
$nextDay = clone $date;
$i = 0; // We have 0 future dates to start with
$nextDates = array(); // Empty array to hold the next 3 dates
while ($i < 3)
{
    $nextDay->add('P1D'); // Add 1 day
    if (in_array($nextDay->format('m-d'), $holidays)) continue; // Don't include year to ensure the check is year independent
    // Note that you may need to do more complicated things for special holidays that don't use specific dates like "the last Friday of this month"
    if (in_array($nextDay->format('D'), $weekend)) continue;
    // These next lines will only execute if continue isn't called for this iteration
    $nextDates[] = $nextDay->format('Y-m-d');
    $i++;
}

isset() O(1) O(n):

$holidays = array('12-24' => '', '12-25' => '');
$weekend = array('Sun' => '','Sat' => '');
$date = new DateTime('2013-12-23');
$nextDay = clone $date;
$i = 0;
$nextDates = array();
while ($i < 3)
{
    $nextDay->add('P1D');
    if (isset($holidays[$nextDay->format('m-d')])) continue;
    if (isset($weekend[$nextDay->format('D')])) continue;
    $nextDates[] = $nextDay->format('Y-m-d');
    $i++;
}
+4

, ; :

function number_of_working_dates($from, $days) {
    $workingDays = [1, 2, 3, 4, 5]; # date format = N (1 = Monday, ...)
    $holidayDays = ['*-12-25', '*-01-01', '2013-12-24', '2013-12-25']; # variable and fixed holidays

    $from = new DateTime($from);
    $dates = [];
    $dates[] = $from->format('Y-m-d');
    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;

        $dates[] = $from->format('Y-m-d');
        $days--;
    }
    return $dates;
}

print_r( number_of_working_dates('2013-12-23', 3) );

+2

All Articles