计算多少工作日后的日期

这是一个PHP类库,用于计算指定日期之后的若干个工作日,考虑了节假日和周末。原始代码在TP框架中遇到问题,通过将DateTime对象替换为time()时间戳解决了不兼容问题。调整后的代码能够正确计算出120个工作日后的日期。
摘要由CSDN通过智能技术生成
<?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(120); // 120个工作日后的时间

$afterBusinessDay=$calculator->getDate();
echo $afterBusinessDay;

这是来自于网络的,本地测试正常改写成TP扩展类就出现错误。

发现TP对datetime 支持性有点问题,改成time()时间缀械式。

<?php

class BusinessDaysCalculator {

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

    /**
     * @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(int $startDate, array $holidays, array $nonBusinessDays ,array $specialBusinessDay) {
        $this->d = $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->d = $this->d+1*24*60*60;
            if ($this->isBusinessDay($this->d)) {
                $i++;
            }
        }
    }

    public function getDate() {
        return $this->d;
    }

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

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

        foreach ($this->holidays as $day) {
            if (date('Y-m-d',$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(
    time(), //当前时间
    $holidays,
    [BusinessDaysCalculator::SATURDAY, BusinessDaysCalculator::SUNDAY],
    $specialBusinessDay
);

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

$afterBusinessDay=$calculator->getDate();
echo date('Y-m-d H:i:s',$afterBusinessDay);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值