0

I'm trying to work out how to invert my date period without pushing the data into an array and reversing it.

Say I would like a list of the last 5 Saturdays the following code will produce:

$begin = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$begin->modify("- 4 weeks");
$end = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$end = $end->modify( '+1 day' );

$interval = new DateInterval('P1W');
$periods = new DatePeriod($begin, $interval ,$end);

foreach($periods as $date){
    echo $date->format("Y-m-d") . "\n";
}
  • 2021-07-31
  • 2021-08-07
  • 2021-08-14
  • 2021-08-21
  • 2021-08-28

I would like this list reversed. So I figured I could make use of the DatePeriod invert property. Both the start and end of the DatePeriod look correct, with a result showing in the current element. However, interating over $period returns nothing:

$begin = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$begin = $begin->modify( '+1 day' );
$end = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$end->modify("- 4 weeks");

$interval = new DateInterval('P1W');
$interval->invert = 1;

$periods = new DatePeriod($begin, $interval ,$end);

foreach($periods as $date){
    echo $date->format("Y-m-d") . "\n";
}

Update

So I've found a solution that calculates a diff of days and passes that to the end param in the DatePeriod constructor. It's not ideal but is working. Note the diff->days had to be divided by the days interval to prevent additional dates pulling through.

$start = new \DateTime( date('Y-m-d', strtotime("last Saturday")));
$end = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$end->modify("- 4 weeks");
$diff = $end->diff($start);
$interval = new \DateInterval('PT0S'); //0 duration
$interval->d = -7; //negative value

$period = new \DatePeriod($start, $interval, $diff->days / 7);
foreach ($period as $date) {
    echo $date->format('Y-m-d') . PHP_EOL;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
atoms
  • 2,993
  • 2
  • 22
  • 43
  • Have you tried `$periods = new DatePeriod($end, $interval ,$begin);`? – Justinas Sep 01 '21 at 12:04
  • i have yes. It's fustraiting as I can't make sense of why it wouldnt be working. Almost like its ignoring the invert flag and attempting to count up still – atoms Sep 01 '21 at 12:05
  • 1
    Does this answer your question? [php reverse order of time period in foreach](https://stackoverflow.com/questions/25333722/php-reverse-order-of-time-period-in-foreach) – Justinas Sep 01 '21 at 12:08
  • I dont beleive it does. – atoms Sep 01 '21 at 12:09
  • 2
    Not even this answer in particular? https://stackoverflow.com/a/35857118/1427878 – CBroe Sep 01 '21 at 12:19
  • @CBroe it half worked, I've had to adjust the $diff->days so its divided by the amount of interval days. Thank you – atoms Sep 01 '21 at 12:22
  • @Justinas thank you. That question did help. Had discarded it to start with. Am still confused as to why my earlier solution wasn't working – atoms Sep 01 '21 at 12:22

0 Answers0