Несколько примеров как получить PHP-массив с интервалом часов – 09:00, 10:00, 11:00 и т.д.
1
Массив с интервалом одного дня
$period = new DatePeriod(
new DateTime('09:00'),
new DateInterval('PT1H'),
new DateTime('18:01')
);
$dates = array();
foreach ($period as $key => $value) {
$dates[] = $value->format('H:00');
}
print_r($dates);
Результат:
Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
[5] => 14:00
[6] => 15:00
[7] => 16:00
[8] => 17:00
[9] => 18:00
)
Интервал с шагом в 30 минут
$period = new DatePeriod(
new DateTime('09:00'),
new DateInterval('PT30M'),
new DateTime('18:01')
);
$dates = array();
foreach ($period as $key => $value) {
$dates[] = $value->format('H:i');
}
print_r($dates);
Результат:
Array
(
[0] => 09:00
[1] => 09:30
[2] => 10:00
[3] => 10:30
[4] => 11:00
[5] => 11:30
[6] => 12:00
[7] => 12:30
[8] => 13:00
[9] => 13:30
[10] => 14:00
[11] => 14:30
[12] => 15:00
[13] => 15:30
[14] => 16:00
[15] => 16:30
[16] => 17:00
[17] => 17:30
[18] => 18:00
)
2
Массив c интервалом в несколько дней
$period = new DatePeriod(
new DateTime('28.03.2022 18:00'),
new DateInterval('PT1H'),
new DateTime('29.03.2022 09:01')
);
$dates = array();
foreach ($period as $key => $value) {
$dates[] = $value->format('d.m.Y H:00');
}
print_r($dates);
Результат:
Array
(
[0] => 28.03.2022 18:00
[1] => 28.03.2022 19:00
[2] => 28.03.2022 20:00
[3] => 28.03.2022 21:00
[4] => 28.03.2022 22:00
[5] => 28.03.2022 23:00
[6] => 29.03.2022 00:00
[7] => 29.03.2022 01:00
[8] => 29.03.2022 02:00
[9] => 29.03.2022 03:00
[10] => 29.03.2022 04:00
[11] => 29.03.2022 05:00
[12] => 29.03.2022 06:00
[13] => 29.03.2022 07:00
[14] => 29.03.2022 08:00
[15] => 29.03.2022 09:00
)