php如何计算指定工作日后的日期?
天蓬老师
天蓬老师 2017-04-11 09:22:59
[PHP讨论组]

例如:今天是2017-1-24 计算20个工作日后的时间

工作日就是周一到周五、法定假期前后调休需上班的都算作工作日,周六、周日和法定假日则不算工作日。

有什么好的办法吗

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(4)
PHP中文网

提供个思路。
1、从聚合数据上“获取当前假期列表API”抓取出当年的假期并存储到表里;
2、从stackexchange.com借鉴了一个“计算指定工作日后的日期”的类库,参考修改如下(1步骤获取的假期列表为此类库的一个参数$holidays):

<?php

class BusinessDaysCalculator {

    const MONDAY    = 1;
    const TUESDAY   = 2;
    const WEDNESDAY = 3;
    const THURSDAY  = 4;
    const FRIDAY    = 5;
    const SATURDAY  = 6;
    const SUNDAY    = 7;

    /**
     * @param DateTime   $startDate       Date to start calculations from
     * @param DateTime[] $holidays        Array of holidays, holidays are no conisdered business days.
     * @param DateTime[]      $nonBusinessDays Array of days of the week which are not business days.
     *  @param DateTime[]      $specialBusinessDay Array is the special work day.
     */
    public function __construct(DateTime $startDate, array $holidays, array $nonBusinessDays ,array $specialBusinessDay) {
        $this->date = $startDate;
        $this->holidays=[];
        foreach($holidays as $holiday){
            array_push($this->holidays,new DateTime($holiday));
        }
        $this->nonBusinessDays = $nonBusinessDays;
        $this->specialBusinessDay = $specialBusinessDay;
    }

    public function addBusinessDays($howManyDays) {
        $i = 0;
        while ($i < $howManyDays) {
            $this->date->modify("+1 day");
            if ($this->isBusinessDay($this->date)) {
                $i++;
            }
        }
    }

    public function getDate() {
        return $this->date->format('Y-m-d');
    }

    private function isBusinessDay(DateTime $date) {
        if(in_array($date->format('Y-m-d') , $this->specialBusinessDay)) return true; //判断当前日期是否是因法定节假日调休而上班的周末,这种情况也算工作日

        if (in_array((int)$date->format('N'), $this->nonBusinessDays)) {
            return false; //当前日期是周末
        }

        foreach ($this->holidays as $day) {
            if ($date->format('Y-m-d') == $day->format('Y-m-d')) {
                return false; //当前日期是法定节假日
            }
        }

        return true;
    }

}

$holidays=["2017-01-27","2017-01-28","2017-01-29","2017-01-30","2017-01-31","2017-02-01","2017-02-02"];//从聚合数据接口获得
$specialBusinessDay=["2017-01-22"];//因法定节假日调休而上班的周末,这种情况也算工作日.因为这种情况少,可以通过手动配置
$calculator = new BusinessDaysCalculator(
    new DateTime(), //当前时间
    $holidays,
    [BusinessDaysCalculator::SATURDAY, BusinessDaysCalculator::SUNDAY],
    $specialBusinessDay
);

$calculator->addBusinessDays(2); // 2个工作日后的时间

$afterBusinessDay=$calculator->getDate();
echo $afterBusinessDay;
天蓬老师

懒人不爱写逻辑,给你个API日历api

PHPz

工作日不是自然日,是指定了的。法定节假日是由国家指定的,机器没法知道,所以需要你也去干预指定。

PHP中文网

如果是想根据工作日与非工作日来做不同的事情, 写个时间路由表。根据时间录入,指定为工作日或非工作日类型。 php直接读表数据。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号