PHP calculate a date based on function for working days

12,538

Solution 1

I did a similar thing a while back using the below function. the key here is skipping the weekends, you can extend this to skip holidays as well.

Example:

Call the function - > addDays(strtotime($startDate), 20, $skipdays,$skipdates = array())

 <?php
    function addDays($timestamp, $days, $skipdays = array("Saturday", "Sunday"), $skipdates = NULL) {
        // $skipdays: array (Monday-Sunday) eg. array("Saturday","Sunday")
        // $skipdates: array (YYYY-mm-dd) eg. array("2012-05-02","2015-08-01");
       //timestamp is strtotime of ur $startDate
        $i = 1;

        while ($days >= $i) {
            $timestamp = strtotime("+1 day", $timestamp);
            if ( (in_array(date("l", $timestamp), $skipdays)) || (in_array(date("Y-m-d", $timestamp), $skipdates)) )
            {
                $days++;
            }
            $i++;
        }

        return $timestamp;
        //return date("m/d/Y",$timestamp);
    }
    ?>

[Edit] : Just read an amazing article on nettuts, Hope this helps http://net.tutsplus.com/tutorials/php/dates-and-time-the-oop-way/

Solution 2

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());

Solution 3

From @this.lau_ answer a rewrite simpler and more logical algorithm, with dynamic (fixed) holydays

public function addBusinessDays($date, $days) {

    $output = new DateTime();
    $output->setTimestamp($date);

    while ($days > 0) {

        $output = $output->add(new DateInterval('P1D'));
        $weekDay = $output->format('N');
        $strDate = $output->format('Y-m-d');

        // Skip Saturday and Sunday
        if ($weekDay == 6 || $weekDay == 7) {

            continue;

        }

        // Skip holidays
        $holidays = $this->_getHolidays();            

        foreach ($holidays as $holiday_date => $holiday_name) {

            if ($holiday_date == $strDate) {

                continue 2;

            }

        }

        $days--;

    }

    return $output->getTimestamp();

}



public function _getHolidays() {

    $feste = array(
        date("Y") . "-01-01" => "Capodanno", 
        date("Y") . "-01-06" => "Epifania", 
        date("Y") . "-04-25" => "Liberazione", 
        date("Y") . "-05-01" => "Festa Lavoratori", 
        date("Y") . "-06-02" => "Festa della Repubblica", 
        date("Y") . "-08-15" => "Ferragosto", 
        date("Y") . "-11-01" => "Tutti Santi", 
        date("Y") . "-12-08" => "Immacolata", 
        date("Y") . "-12-25" => "Natale", 
        date("Y") . "-12-26" => "St. Stefano"
    );

    return $feste;

}

Call the function with

$deliveryDate = addBusinessDays(time(), 7);
Share:
12,538
David
Author by

David

Updated on June 07, 2022

Comments

  • David
    David about 2 years

    I am using this function (that I found on this forum) to calculate the number of working days between a range:

    <?php
    //The function returns the no. of business days between two dates and it skips the holidays
    function getWorkingDays($startDate,$endDate,$holidays){
    // do strtotime calculations just once
    $endDate = strtotime($endDate);
    $startDate = strtotime($startDate);
    
    
    //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
    //We add one to inlude both dates in the interval.
    $days = ($endDate - $startDate) / 86400 + 1;
    
    $no_full_weeks = floor($days / 7);
    $no_remaining_days = fmod($days, 7);
    
    //It will return 1 if it's Monday,.. ,7 for Sunday
    $the_first_day_of_week = date("N", $startDate);
    $the_last_day_of_week = date("N", $endDate);
    
    //---->The two can be equal in leap years when february has 29 days, the equal sign is added here
    //In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
    if ($the_first_day_of_week <= $the_last_day_of_week) {
        if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
        if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
    }
    else {
        // (edit by Tokes to fix an edge case where the start day was a Sunday
        // and the end day was NOT a Saturday)
    
        // the day of the week for start is later than the day of the week for end
        if ($the_first_day_of_week == 7) {
            // if the start date is a Sunday, then we definitely subtract 1 day
            $no_remaining_days--;
    
            if ($the_last_day_of_week == 6) {
                // if the end date is a Saturday, then we subtract another day
                $no_remaining_days--;
            }
        }
        else {
            // the start date was a Saturday (or earlier), and the end date was (Mon..Fri)
            // so we skip an entire weekend and subtract 2 days
            $no_remaining_days -= 2;
        }
    }
    
    //The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
    //---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
    $workingDays = $no_full_weeks * 5;
    if ($no_remaining_days > 0 )
    {
      $workingDays += $no_remaining_days;
    }
    
    //We subtract the holidays
    foreach($holidays as $holiday){
        $time_stamp=strtotime($holiday);
        //If the holiday doesn't fall in weekend
        if ($startDate <= $time_stamp && $time_stamp <= $endDate && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
            $workingDays--;
    }
    
    return $workingDays;
    }
    
    //Example:
    
    $holidays=array("2008-12-25","2008-12-26","2009-01-01");
    
    echo getWorkingDays("$startdate","$enddate",$holidays)
    ?>
    

    Now I'd like to extend this function a bit. I would like to generate what date it will be if I add X working days from the starting date. Say for example i have a variable that holds the value 20

    $workingdays = "20";
    

    And the $startdate is 2012-06-01 i would like to make this function calculate that the startdate + 20 working days will be 2012-06-28. Would this be possible?

  • David
    David about 12 years
    Can you please explain a little more on how to use it? I'm very new to PHP functions in general.
  • Eswar Rajesh Pinapala
    Eswar Rajesh Pinapala about 12 years
    Call the function - > addDays(strtotime($startDate), 20, $skipdays,$skipdates = NULL); its ok if you send empty vals for $skipDays, and $skipDates, in this case by defauld Sat, sun and no extra Dates will be skipped
  • David
    David about 12 years
    How can I convert that timestamp thingy to an actual Y-M-D date? Yes I'm a newbie..
  • David
    David about 12 years
    There seems to be some problem with $startdate as variable.. It works if I manually use strtotime(2012-06-01) but strtotime($startdate) don't. Even though if i echo $startdate; it outputs the correct date...
  • Eswar Rajesh Pinapala
    Eswar Rajesh Pinapala about 12 years
    juggling problem? can you do a quick test, can you do type casting? do: $startDate = (string)$startDate; echo strtotime($startDate);
  • SigmaSteve
    SigmaSteve almost 9 years
    Thank you, I'm going to use this but return $days instead so I know the actual number of days when the weekends aren't counted.
  • LucScu
    LucScu over 8 years
    try to make all holidays dynamic :)
  • LucScu
    LucScu over 8 years
    there is a bug in the foreach $holidays. The continue is in the foreach scope and not in while scope, you have to use continue 2.
  • LucScu
    LucScu over 8 years
    your welcome, i like your algorithm and i used it with minor modifications. thx a lot you too.