php reverse order of time period in foreach
Using a negative one day interval and the difference of days between the two dates you can accomplish the same thing without needing to use array_reverse(iterator_to_array())
or build another array.
Example: https://3v4l.org/B3nNk
$start = new \DateTime('2014-08-15');
$end = new \DateTime('2014-08-09');
$diff = $end->diff($start);
$interval = \DateInterval::createFromDateString('-1 day');
/*
//alternative usage
$interval = new \DateInterval('PT0S'); //0 duration
$interval->d = -1; //negative value
*/
$period = new \DatePeriod($start, $interval, $diff->days);
foreach ($period as $date) {
echo $date->format('Y-m-d') . PHP_EOL;
}
Result:
2014-08-15
2014-08-14
2014-08-13
2014-08-12
2014-08-11
2014-08-10
2014-08-09
In this way it tells DatePeriod
to subtract 1 day, for the number of days returned from the diff, instead of adding up until the end date.
Daylight Savings Time Bug (PHP < 8.1)
NOTE: This approach does not work for intervals less than a day with time-zones that recognize DST, due to a known issue with dates and the DST hour changes. https://bugs.php.net/bug.php?id=74274, which extends to
DateTime::sub()
, and similar usages. Fixed in PHP 8.1
Example https://3v4l.org/2DKBs
date_default_timezone_set('America/New_York');
$start = new DateTime('2020-03-08 04:00');
$interval = DateInterval::createFromDateString('-1 hour');
$period = new DatePeriod($start, $interval, 10);
foreach ($period as $date) {
echo $date->format('Y-m-d h:i a') . PHP_EOL;
}
Result
2020-03-08 04:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
2020-03-08 03:00 am
DST Workaround and Best-Practice
The workaround is to use a time-zone that does not recognize DST, such as UTC. Using the UTC time-zone for internal application dates and converting to the desired time-zone for display, is currently considered best-practice.
Example https://3v4l.org/3YflE
date_default_timezone_set('UTC');
$start = new DateTime('2020-03-08 04:00');
$interval = DateInterval::createFromDateString('-1 hour');
$period = new DatePeriod($start, $interval, 10);
foreach ($period as $date) {
echo $date->format('Y-m-d h:i a') . PHP_EOL;
}
Result
2020-03-08 04:00 am
2020-03-08 03:00 am
2020-03-08 02:00 am
2020-03-08 01:00 am
2020-03-08 12:00 am
2020-03-07 11:00 pm
2020-03-07 10:00 pm
2020-03-07 09:00 pm
2020-03-07 08:00 pm
2020-03-07 07:00 pm
2020-03-07 06:00 pm
$period = array_reverse(iterator_to_array($period));