Calculate business days

后端 未结 30 2134
猫巷女王i
猫巷女王i 2020-11-22 05:16

I need a method for adding \"business days\" in PHP. For example, Friday 12/5 + 3 business days = Wednesday 12/10.

At a minimum I need the code to understand weekend

30条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 06:02

    This code snippet is very easy to calculate business day without week end and holidays:

    function getWorkingDays($startDate,$endDate,$offdays,$holidays){
    $endDate = strtotime($endDate);
    $startDate = strtotime($startDate);
    $days = ($endDate - $startDate) / 86400 + 1;
    $counter=0;
    for ($i = 1; $i <= $days; $i++) {
        $the_first_day_of_week = date("N", $startDate);
        $startDate+=86400;
    if (!in_array($the_first_day_of_week, $offdays) && !in_array(date("Y-m-
    d",$startDate), $holidays)) {
    $counter++;
    }
    
    }   
    return $counter;
    }
    //example to use
    $holidays=array("2017-07-03","2017-07-20");
    $offdays=array(5,6);//weekend days Monday=1 .... Sunday=7
    echo getWorkingDays("2017-01-01","2017-12-31",$offdays,$holidays)
    

提交回复
热议问题