Calculate business days

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

    Variant 1:

    0) {
            $timestamp += 86400;
            if (date('N', $timestamp)<6) $bDays--;
        }
        return $timestamp;
    }
    

    Variant 2:

    0) {
            $timestamp += 86400;
            if (date('N', $timestamp)<6) $bDays--;
        }
        return $timestamp;
    }
    

    Variant 3:

    0) {
            $timestamp += 86400;
            if (date('N', $timestamp)<6) $bDays--;
        }
        return $timestamp += 86400;
    }
    

    The additional holiday considerations can be made using variations of the above by doing the following. Note! assure all the timestamps are the same time of the day (i.e. midnight).

    Make an array of holiday dates (as unixtimestamps) i.e.:

    $holidays = array_flip(strtotime('2011-01-01'),strtotime('2011-12-25'));
    

    Modify line :

    if (date('N', $timestamp)<6) $bDays--;
    

    to be :

    if (date('N', $timestamp)<6 && !isset($holidays[$timestamp])) $bDays--;
    

    Done!

    0) {
            $timestamp += 86400;
            if (date('N', $timestamp)<6 && !isset($holidays[$timestamp])) $bDays--;
        }
        return $timestamp;
    }
    

提交回复
热议问题