Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created June 30, 2011 16:35
Show Gist options
  • Save jboesch/1056614 to your computer and use it in GitHub Desktop.
Save jboesch/1056614 to your computer and use it in GitHub Desktop.
Loop over dates in PHP 5.3+
<?
// Looping over date ranges, requires PHP 5.3+
$begin = new DateTime('2011-01-05');
$end = new DateTime('2011-01-10');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$dates = array();
foreach($period as $dt)
{
$dates[] = $dt->format("Y-m-d");
}
// print_r($dates);
// 2011-01-05, 2011-01-06, 2011-01-07, 2011-01-08, 2011-01-09, 2011-01-10
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment