I have a time interval which consists of two DateTimes:
array(
"DATE_TIME_FROM" => new DateTime(...) , //2021-01-01 10:00
"DATE_TIME_TO" => new DateTime(...), //2021-01-02 01:00
)
If any of these datetimes go beyound the day then an existing array should be split into two like I shown in an example below:
array(
"DATE_TIME_FROM" => new DateTime(...) , //2021-01-01 10:00
"DATE_TIME_TO" => new DateTime(...), //2021-01-02 00:00
),
array(
"DATE_TIME_FROM" => new DateTime(...) , //2021-01-02 00:00
"DATE_TIME_TO" => new DateTime(...), //2021-01-02 01:00
)
Is it possible somehow? Thanks in advance!
2
Answers
You can iterate through a
DatePeriod
with aDateInterval
of 1 day from startDateTime
to endDateTime
:Get the midnight of DATE_TIME_TO:
If it is greater than DATE_TIME_FROM, split the array into two: