Skip to content

Instantly share code, notes, and snippets.

@johnabela
Created March 29, 2016 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnabela/56d080ecf4778da4154d to your computer and use it in GitHub Desktop.
Save johnabela/56d080ecf4778da4154d to your computer and use it in GitHub Desktop.
$start = new DateTime('now');
$end = new DateTime('- 7 day');
$diff = $end->diff($start);
$interval = DateInterval::createFromDateString('-1 day');
$period = new DatePeriod($start, $interval, $diff->days);
foreach ($period as $date) {
echo $date->format('m-d') . '<br>'; // or whatever you want to do
}
@mehov
Copy link

mehov commented Oct 22, 2020

Can we not just use $end as the last argument? That way there's no need to add clutter with $diff.

public __construct ( DateTimeInterface $start , DateInterval $interval , DateTimeInterface $end [, int $options ] )

(According to /manual/en/dateperiod.construct.php)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment