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

前端 未结 3 1623
青春惊慌失措
青春惊慌失措 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:27

    A function based on Tinh Dang's answer:

    function getFutureBusinessDay($num_business_days, $today_ymd = null, $holiday_dates_ymd = []) {
        $num_business_days = min($num_business_days, 1000);
        $business_day_count = 0;
        $current_timestamp = empty($today_ymd) ? time() : strtotime($today_ymd);
        while ($business_day_count < $num_business_days) {
            $next1WD = strtotime('+1 weekday', $current_timestamp);
            $next1WDDate = date('Y-m-d', $next1WD);        
            if (!in_array($next1WDDate, $holiday_dates_ymd)) {
                $business_day_count++;
            }
            $current_timestamp = $next1WD;
        }
        return date('Y-m-d', $current_timestamp);
    }
    

    I made it limit the loop to 1000 business days. There could be no limit if desired.

提交回复
热议问题