Calculate business days

后端 未结 30 1853
猫巷女王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 05:56

    I just get my function working based on Bobbin and mcgrailm code, adding some things that worked perfect to me.

    function add_business_days($startdate,$buisnessdays,$holidays,$dateformat){
        $enddate = strtotime($startdate);
        $day = date('N',$enddate);
        while($buisnessdays > 0){ // compatible with 1 businessday if I'll need it
            $enddate = strtotime(date('Y-m-d',$enddate).' +1 day');
            $day = date('N',$enddate);
            if($day < 6 && !in_array(date('Y-m-d',$enddate),$holidays))$buisnessdays--;
        }
        return date($dateformat,$enddate);
    }
    
    // as a parameter in in_array function we should use endate formated to 
    // compare correctly with the holidays array.
    

提交回复
热议问题