工作日的计算需要根据每年的具体情况设置编写年份的json文件(设定法定放假日期与调休上班日期),json文件中0代表假期,1、代表调休上班
<?php
use DateTime;
use Exception;
use DatePeriod;
use DateInterval;
/**
* @description: 工作日
*/
class WorkingDay
{
/**
* @description: 特殊日期,节假日,调休日
* @var array
*/
private $_specialDay = [];
/**
* @description: 计算指定日期N个工作日之后的日期
* @param string $date 指定日期
* @param int $interval 间隔过昨日
* @return {*}
*/
public function workDays($date, $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 $date1 指定日期1
* @param string $date2 指定日期2
* @return {*}
*/
public function dayInterval($date1, $date2)
{
$workDay = 0;
$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++;
}
}
return $workDay;
}
/**
* @description: 判断某一天是否是工作日
* @param string $date
* @return bool
*/
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' ? false : true;
}
}
}
2202.json
{
"2022-01-01": 0,
"2022-01-02": 0,
"2022-01-03": 0,
"2022-01-29": 1,
"2022-01-30": 1,
"2022-01-31": 0,
"2022-02-01": 0,
"2022-02-02": 0,
"2022-02-03": 0,
"2022-02-04": 0,
"2022-02-05": 0,
"2022-02-06": 0,
"2022-04-02": 1,
"2022-04-03": 0,
"2022-04-04": 0,
"2022-04-05": 0,
"2022-04-24": 1,
"2022-04-30": 0,
"2022-05-01": 0,
"2022-05-02": 0,
"2022-05-03": 0,
"2022-05-04": 0,
"2022-05-07": 1,
"2022-06-03": 0,
"2022-06-04": 0,
"2022-06-05": 0,
"2022-09-10": 0,
"2022-09-11": 0,
"2022-09-12": 0,
"2022-10-01": 0,
"2022-10-02": 0,
"2022-10-03": 0,
"2022-10-04": 0,
"2022-10-05": 0,
"2022-10-06": 0,
"2022-10-07": 0,
"2022-10-08": 1,
"2022-10-09": 1
}
2021.json
{
"2021-01-01": 0,
"2021-01-02": 0,
"2021-01-03": 0,
"2021-02-07": 1,
"2021-02-11": 0,
"2021-02-12": 0,
"2021-02-13": 0,
"2021-02-14": 0,
"2021-02-15": 0,
"2021-02-16": 0,
"2021-02-17": 0,
"2021-02-20": 1,
"2021-04-03": 0,
"2021-04-04": 0,
"2021-04-05": 0,
"2021-04-25": 1,
"2021-05-01": 0,
"2021-05-02": 0,
"2021-05-03": 0,
"2021-05-04": 0,
"2021-05-05": 0,
"2021-05-08": 1,
"2021-06-12": 0,
"2021-06-13": 0,
"2021-06-14": 0,
"2021-09-18": 1,
"2021-09-19": 0,
"2021-09-20": 0,
"2021-09-21": 0,
"2021-09-26": 1,
"2021-10-01": 0,
"2021-10-02": 0,
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": 0,
"2021-10-07": 0,
"2021-10-09": 1
}