1 回答
TA贡献1775条经验 获得超11个赞
使用DateTime
$start = new \DateTime($model->startDate); //2020-01-01
$end = new \DateTime($model->endDate); //2020-01-31
$endDate = $end->format('Y-m-d');
$interval = new \DateInterval('P1D');
$end->add($interval);
$period = new \DatePeriod($start, $interval, $end);
foreach ($period as $date) {
$allDates[] = [
'date' => $date->format('Y-m-d'),
'dayNo' => $date->format('N'),
];
}
//Initialized public holiday
$holidays = HolidayModelName::find()
->select('date')
->where(['between', 'date', $start->format('Y-m-d'), $endDate])
->indexBy('date')
->column();
$workDay = 0;
foreach ($allDates as $value) {
$isWeekOff = $value['dayNo'] == 6 || $value['dayNo'] == 7;
if (!$isWeekOff && !isset($holidays[$value['date']])) {
$workDay++;
}
}
// Result : 20 Work Days
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报