note 127977 added to class.dateinterval
| From: | cb at main dot php dot net | Date: | Sun, 04 Dec 2022 04:12:17 +0000 |
| Subject: | note 127977 added to class.dateinterval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219933@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);
function show_dates ($dr) {
foreach($dr as $date1){
echo $date1->format('Y-m-d').'<br>';
}
}
show_dates ($daterange);
echo '<br>';
// reverse the array
$daterange = array_reverse(iterator_to_array($daterange));
show_dates ($daterange);
?>
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.91.159.161 (proxied: 2407:7000:81a2:a000:8df:9e38:f3ba:8496)
----
Manual Page -- https://php.net/manual/en/class.dateinterval.php
Edit -- https://main.php.net/note/edit/127977
Del: integrated -- https://main.php.net/note/delete/127977/integrated
Del: useless -- https://main.php.net/note/delete/127977/useless
Del: bad code -- https://main.php.net/note/delete/127977/bad+code
Del: spam -- https://main.php.net/note/delete/127977/spam
Del: non-english -- https://main.php.net/note/delete/127977/non-english
Del: in docs -- https://main.php.net/note/delete/127977/in+docs
Del: other reasons-- https://main.php.net/note/delete/127977
Reject -- https://main.php.net/note/reject/127977
Search -- https://main.php.net/manage/user-notes.php