note 126658 added to class.dateinterval
| From: | cb at main dot php dot net | Date: | Thu, 02 Dec 2021 21:15:08 +0000 |
| Subject: | note 126658 added to class.dateinterval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217324@lists.php.net to get a copy of this message | ||
If you want to reverse a date interval use array_reverse and iterator_to_array. I've found
using invert to be unreliable.
<?php
$start_date = date_create("2021-01-01");
$end_date = date_create("2021-01-05"); // If you want to include this date, add 1 day
$interval = DateInterval::createFromDateString('1 day');
$daterange = new DatePeriod($start_date, $interval ,$end_date);
foreach($daterange as $date1){
echo $date1->format('Y-m-d').'<br>';
}
echo '<br>';
// reverse the array
$daterange = array_reverse(iterator_to_array($daterange));
foreach($daterange as $date1){
echo $date1->format('Y-m-d').'<br>';
}
?>
Gives
2021-01-01
2021-01-02
2021-01-03
2021-01-04
2021-01-04
2021-01-03
2021-01-02
2021-01-01
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 2407:7000:81a2:a000:d4e:f97a:51ad:7a57)
----
Manual Page -- https://php.net/manual/en/class.dateinterval.php
Edit -- https://main.php.net/note/edit/126658
Del: integrated -- https://main.php.net/note/delete/126658/integrated
Del: useless -- https://main.php.net/note/delete/126658/useless
Del: bad code -- https://main.php.net/note/delete/126658/bad+code
Del: spam -- https://main.php.net/note/delete/126658/spam
Del: non-english -- https://main.php.net/note/delete/126658/non-english
Del: in docs -- https://main.php.net/note/delete/126658/in+docs
Del: other reasons-- https://main.php.net/note/delete/126658
Reject -- https://main.php.net/note/reject/126658
Search -- https://main.php.net/manage/user-notes.php