Php add 5 working days to current date excluding weekends (sat-sun) and excluding (multiple) holidays

前端 未结 3 1575
青春惊慌失措
青春惊慌失措 2021-02-11 11:23

For delivery of our webshop, we need to calculate 5 working days from the current date in php.

Our working days are from monday to friday and we have several closing day

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-11 12:10

    You can use the "while statement", looping until get enough 5 days. Each time looping get & check one next day is in the holiday list or not.

    Here is the the example:

    $holidayDates = array(
        '2016-03-26',
        '2016-03-27',
        '2016-03-28',
        '2016-03-29',
        '2016-04-05',
    );
    
    $count5WD = 0;
    $temp = strtotime("2016-03-25 00:00:00"); //example as today is 2016-03-25
    while($count5WD<5){
        $next1WD = strtotime('+1 weekday', $temp);
        $next1WDDate = date('Y-m-d', $next1WD);
        if(!in_array($next1WDDate, $holidayDates)){
            $count5WD++;
        }
        $temp = $next1WD;
    }
    
    $next5WD = date("Y-m-d", $temp);
    
    echo $next5WD; //if today is 2016-03-25 then it will return 2016-04-06 as many days between are holidays
    

提交回复
热议问题