Calculate business days

后端 未结 30 1854
猫巷女王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:07

    Personally, I think this is a cleaner and more concise solution:

    function onlyWorkDays( $d ) {
        $holidays = array('2013-12-25','2013-12-31','2014-01-01','2014-01-20','2014-02-17','2014-05-26','2014-07-04','2014-09-01','2014-10-13','2014-11-11','2014-11-27','2014-12-25','2014-12-31');
        while (in_array($d->format("Y-m-d"), $holidays)) { // HOLIDAYS
            $d->sub(new DateInterval("P1D"));
        }
        if ($d->format("w") == 6) { // SATURDAY
            $d->sub(new DateInterval("P1D"));
        }
        if ($d->format("w") == 0) { // SUNDAY
            $d->sub(new DateInterval("P2D"));
        }
        return $d;
    }
    

    Just send the proposed new date to this function.

提交回复
热议问题