<?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'));
03-15
5539
07-16
860
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交