欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

日期推迟算法,工作日顺延

程序员文章站 2022-05-18 07:56:19
...
date_default_timezone_set('UTC');
$config = [
    '20170527' => -1,
    '20170529' => 2,
    '20170930' => -1,
    '20171002' => 5,
];
function delayDate($now, $days, $config){
    for($i =1; $i <= $days; $i++){
        $cur_date = date('Ymd', strtotime($now) + 86400 * $i);
        $week = date('w',strtotime($cur_date));
        //周六日顺延
        if($week == 0 || $week == 6){
            $days++;
        }
        //特殊节假日顺延,涵盖周末上班情况
        if(array_key_exists($cur_date, $config)){
            $days = $days + $config[$cur_date];
        }
    }
    $end_date = date('Ymd',strtotime($now)+86400*$days);
    return $end_date;
}
echo delayDate('20170928', 3, $config); //20171009
echo '<br>';
echo delayDate('20171001', 3, $config); //20171011
echo '<br>';
echo delayDate('20170927', 3, $config); //20170930
echo '<br>';
echo delayDate('20170925', 3, $config); //20170928
exit;