Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php reverse order of time period in foreach

hi i have script gives me list of days date between a week. i need the reverse the the order. alredy tried rsort, reverse_array, and much more some how all giving error.

thanks.

script

$date2 = "$lastweek2";
$date1 = "$lastweek1";

$start    = new DateTime($date2);
$end      = new DateTime($date1);
$interval = DateInterval::createFromDateString('1 day');
$period   = new DatePeriod($start, $interval, $end);

echo "<br>";
foreach ($period as $dt) {
    echo $dt->format("Y-m-d") . "<br>\n";
}

echo is like this

2014-08-09
2014-08-10
2014-08-11
2014-08-12
2014-08-13
2014-08-14
2014-08-15

reversed

2014-08-15
2014-08-14
2014-08-13
2014-08-12
2014-08-11
2014-08-10
2014-08-09
like image 885
sarabrownD Avatar asked Aug 15 '14 20:08

sarabrownD


4 Answers

$period = array_reverse(iterator_to_array($period));
like image 191
izupet Avatar answered Oct 12 '22 17:10

izupet


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
like image 33
Will B. Avatar answered Oct 12 '22 17:10

Will B.


Just put the dates in an array and then reverse the order of the array using array_reverse()

$dates = array();
foreach ($period as $dt) {
    $dates[] = $dt->format("Y-m-d");
}

$dates = array_reverse($dates);
echo implode("<br>\n", $dates);
like image 30
John Conde Avatar answered Oct 12 '22 17:10

John Conde


$date1 = "2014-08-15";
$date2 = "2014-08-09";

$start    = new DateTime($date2);
$end      = new DateTime($date1);
$i = DateInterval::createFromDateString('1 day');
while ($end >= $start) {
  echo $end->format("Y-m-d") . "<br>\n";
  $end = $end->sub($i);
}
like image 22
MrTux Avatar answered Oct 12 '22 18:10

MrTux