土日祝日を除いた平日の日付リストを日数指定で取得します。
祝日判定には以前作成した祝日取得クラスを利用してみました。
weekdays.php
<?php
require_once('holiday_class.php');
class Weekdays {
private $holiday;
private $startDate;
public function __construct() {
$this->holiday = new Holiday;
$this->startDate = new DateTimeImmutable;
}
public function setStartDate($ymd = 'now') {
$this->startDate = new DateTimeImmutable($ymd);
}
private function isHoliday($date) {
[$y, $n, $j] = explode(',', $date->format('Y,n,j'));
return isset($this->holiday->getHolidayOfYear($y)[$n][$j]);
}
public function getDateList($days = 1) {
$days = max(1, $days);
$currentDate = new DateTime;
$currentDate->setTimestamp($this->startDate->getTimestamp());
$result = [];
do {
while(strpos('06', $currentDate->format('w')) !== false ||
$this->isHoliday($currentDate)) {
$currentDate->modify('+1 day');
}
$result[] = $currentDate->format('Y-m-d');
$currentDate->modify('+1 day');
} while(--$days > 0);
return $result;
}
}
example####
// example
$weekdays = new Weekdays;
$date_list = $weekdays->getDateList(7);
print_r($date_list);
/*
Array
(
[0] => 2021-09-17
[1] => 2021-09-21
[2] => 2021-09-22
[3] => 2021-09-24
[4] => 2021-09-27
[5] => 2021-09-28
[6] => 2021-09-29
)
*/
foreach($date_list as $k => $v) {
printf("%d: %s (%s)\n",
$k, $v, ['日','月','火','水','木','金','土'][date('w', strtotime($v))]
);
}
/*
0: 2021-09-17 (金)
1: 2021-09-21 (火)
2: 2021-09-22 (水)
3: 2021-09-24 (金)
4: 2021-09-27 (月)
5: 2021-09-28 (火)
6: 2021-09-29 (水)
*/
$weekdays->setStartDate('2021-12-20');
$date_list = $weekdays->getDateList(20);
print_r($date_list);
/*
Array
(
[0] => 2021-12-20
[1] => 2021-12-21
[2] => 2021-12-22
[3] => 2021-12-23
[4] => 2021-12-24
[5] => 2021-12-27
[6] => 2021-12-28
[7] => 2021-12-29
[8] => 2021-12-30
[9] => 2021-12-31
[10] => 2022-01-03
[11] => 2022-01-04
[12] => 2022-01-05
[13] => 2022-01-06
[14] => 2022-01-07
[15] => 2022-01-11
[16] => 2022-01-12
[17] => 2022-01-13
[18] => 2022-01-14
[19] => 2022-01-17
)
*/