Wednesday, June 30, 2021

PHP calculate a date based on function for working days for future

If someone's interested, I'm using this function to add X business days to a date. The function takes in a timestamp and returns a timestamp. It's possible to specify the holidays via an array (if in the US, you can use usBankHolidays()).

At the moment, it's assuming Saturday and Sunday are not business days but that can be changed easily.

Code:

function addBusinessDays($date, $days, $holidays = array()) {
    $output = new DateTime();
    $output->setTimestamp($date);
    while ($days > 0) {
        $weekDay = $output->format('N');

        // Skip Saturday and Sunday
        if ($weekDay == 6 || $weekDay == 7) {
            $output = $output->add(new DateInterval('P1D'));
            continue;
        }

        // Skip holidays
        $strDate = $output->format('Y-m-d');
        foreach ($holidays as $s) {
            if ($s == $strDate) {
                $output = $output->add(new DateInterval('P1D'));
                continue 2;
            }
        }

        $days--;
        $output = $output->add(new DateInterval('P1D'));
    }
    return $output->getTimestamp();
}

function usBankHolidays($format = 'datesonly') {
    $output = array(
        array('2015-05-25', 'Memorial Day'),
        array('2015-07-03', 'Independence Day'),
        array('2015-09-07', 'Labor Day'),
        array('2015-10-12', 'Columbus Day'),
        array('2015-11-11', 'Veterans Day'),
        array('2015-11-26', 'Thanksgiving Day'),
        array('2015-12-25', 'Christmas Day'),
        array('2016-01-01', 'New Year Day'),
        array('2016-01-18', 'Martin Luther King Jr. Day'),
        array('2016-02-15', 'Presidents Day (Washingtons Birthday)'),
        array('2016-05-30', 'Memorial Day'),
        array('2016-07-04', 'Independence Day'),
        array('2016-09-05', 'Labor Day'),
        array('2016-10-10', 'Columbus Day'),
        array('2016-11-11', 'Veterans Day'),
        array('2016-11-24', 'Thanksgiving Day'),
        array('2016-12-25', 'Christmas Day'),
        array('2017-01-02', 'New Year Day'),
        array('2017-01-16', 'Martin Luther King Jr. Day'),
        array('2017-02-20', 'Presidents Day (Washingtons Birthday)'),
        array('2017-05-29', 'Memorial Day'),
        array('2017-07-04', 'Independence Day'),
        array('2017-09-04', 'Labor Day'),
        array('2017-10-09', 'Columbus Day'),
        array('2017-11-10', 'Veterans Day'),
        array('2017-11-23', 'Thanksgiving Day'),
        array('2017-12-25', 'Christmas Day'),
        array('2018-01-01', 'New Year Day'),
        array('2018-01-15', 'Martin Luther King Jr. Day'),
        array('2018-02-19', 'Presidents Day (Washingtons Birthday)'),
        array('2018-05-28', 'Memorial Day'),
        array('2018-07-04', 'Independence Day'),
        array('2018-09-03', 'Labor Day'),
        array('2018-10-08', 'Columbus Day'),
        array('2018-11-12', 'Veterans Day'),
        array('2018-11-22', 'Thanksgiving Day'),
        array('2018-12-25', 'Christmas Day'),
        array('2019-01-01', 'New Year Day'),
        array('2019-01-21', 'Martin Luther King Jr. Day'),
        array('2019-02-18', 'Presidents Day (Washingtons Birthday)'),
        array('2019-05-27', 'Memorial Day'),
        array('2019-07-04', 'Independence Day'),
        array('2019-09-02', 'Labor Day'),
        array('2019-10-14', 'Columbus Day'),
        array('2019-11-11', 'Veterans Day'),
        array('2019-11-28', 'Thanksgiving Day'),
        array('2019-12-25', 'Christmas Day'),
        array('2020-01-01', 'New Year Day'),
        array('2020-01-20', 'Martin Luther King Jr. Day'),
        array('2020-02-17', 'Presidents Day (Washingtons Birthday)'),
        array('2020-05-25', 'Memorial Day'),
        array('2020-07-03', 'Independence Day'),
        array('2020-09-07', 'Labor Day'),
        array('2020-10-12', 'Columbus Day'),
        array('2020-11-11', 'Veterans Day'),
        array('2020-11-26', 'Thanksgiving Day'),
        array('2020-12-25', 'Christmas Day '),
    );

    if ($format == 'datesonly') {
        $temp = array();
        foreach ($output as $item) {
            $temp[] = $item[0];
        }
        $output = $temp;
    }

    return $output;
}

Usage:

$deliveryDate = addBusinessDays(time(), 7, usBankHolidays());

Source: https://stackoverflow.com/questions/11130390/php-calculate-a-date-based-on-function-for-working-days/34083964 



How to backup and download Database using PHP

< ?php $mysqlUserName = 'databaseusername' ; $mysqlPassword = 'databasepassword' ; $mysqlHostNa...