init: 项目初始代码
This commit is contained in:
78
extend/utils/BusinessDaysCalculator.php
Normal file
78
extend/utils/BusinessDaysCalculator.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
class BusinessDaysCalculator {
|
||||
|
||||
const MONDAY = 1;
|
||||
const TUESDAY = 2;
|
||||
const WEDNESDAY = 3;
|
||||
const THURSDAY = 4;
|
||||
const FRIDAY = 5;
|
||||
const SATURDAY = 6;
|
||||
const SUNDAY = 7;
|
||||
|
||||
private $date = '';
|
||||
|
||||
/**
|
||||
* @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;
|
||||
Reference in New Issue
Block a user