PHP获取全年工作日

<?php

/**
 * @description=> 工作日
 */
class WorkingDay
{
    /**
     * @description=> 特殊日期,节假日,调休日
     * @var array
     */
    private $_specialDay = [];
	
	/**
	 * https://timor.tech/api/holiday/year/2020/ 指定年份
	 * @var string
	 */
    public $yearAPI = 'http://timor.tech/api/holiday/year';
    
    public function __construct()
    {
        // 获取假期及调休
        $yearJson = file_get_contents($this->yearAPI);
        $yearJson = json_decode($yearJson, 1);
        $year     = date('Y');
        foreach ($yearJson['holiday'] as $v) {
            $this->_specialDay[$year][$v['date']] = (int) !$v['holiday'];
        }
    }
    
    /**
     * @description=> 计算指定日期N个工作日之后的日期
     * @param  string  $date      指定日期
     * @param  int     $interval  间隔过昨日
     * @return string {*}
     * @throws Exception
     */
    public function workDays(string $date, int $interval)
    {
        $datetime     = new DateTime($date);
        $dataInterval = new DateInterval('P1D');
        
        while ($interval > 0) {
            $datetime->add($dataInterval);
            if ($this->judgeDay($datetime->format('Y-m-d'))) {
                $interval--;
            }
        }
        return $datetime->format('Y-m-d');
    }
    
    /**
     * @description=> 判断某一天是否是工作日
     * @param  string  $date
     * @return bool
     * @throws Exception
     */
    public function judgeDay($date)
    {
        $datetime = new DateTime($date);
        $year = $datetime->format('Y');
        if (!isset($this->_specialDay[$year])) {
            $this->_specialDay[$year] = json_decode(file_get_contents(dirname(__FILE__) . '/' . $year . '.json'), true);
            if (!$this->_specialDay[$year]) {
                throw new Exception('请先设置' . $year . '年份假期与调休');
            }
        }
        if (isset($this->_specialDay[$year][$date])) {
            return (bool)$this->_specialDay[$year][$date];
        } else {
            $week = $datetime->format('D');
            return !($week == 'Sat' || $week == 'Sun');
        }
    }
    
    /**
     * @description=> 计算两个日期间的工作日
     * @param  string  $date1  指定日期1
     * @param  string  $date2  指定日期2
     * @return array
     * @throws Exception
     */
    public function dayInterval($date1, $date2)
    {
		$workDay = [];
        $datetime1 = new DateTime($date1);
        $datetime2 = new DateTime($date2);
        $dataInterval = new DateInterval('P1D');
        $datetime2->add($dataInterval);
        $datePeriod = new DatePeriod($datetime1, $dataInterval, $datetime2);
        foreach ($datePeriod as $date) {
            if ($this->judgeDay($date->format('Y-m-d'))) {
                $workDay[] = $date->format('Y-m-d');
            }
        }
        return $workDay;
    }
}

$w = new WorkingDay();
print_r($w->dayInterval('2023-01-01','2023-02-28'));

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值