Generate a list of dates between two dates in PHP
Published by Nicholas Dunbar on March 6th, 2013
If you want to create a list of dates between two different Unix time stamps, here is how to do it:
//2001-09-09T01:46:4
$starting_unix_timestamp = 1000000000;
//now
$ending_unix_timestamp = time();
date_default_timezone_set('America/Denver');
$starting_date = new DateTime(date("Y-m-d",$starting_unix_timestamp));
$ending_date = new DateTime(date("Y-m-d",$ending_unix_timestamp));
$interval = $starting_date->diff($ending_date);
$number_of_days_between = ((int)$interval->format("%r %a"));
$days_between_list = array();$ending_date = new DateTime(date("Y-m-d",$ending_unix_timestamp));
$interval = $starting_date->diff($ending_date);
$number_of_days_between = ((int)$interval->format("%r %a"));
for ($i=1; $i<$number_of_days_between; $i++){
//generate complete list of days between dates
$starting_date->add(new DateInterval('P1D'));
array_push($days_between_list,$starting_date->getTimestamp());
}
print_r($days_between_list);
Compatible with (PHP 5 >= 5.2.0)
to check your PHP version run
phpinfo();
?>
or if you are running the below script from the command line
php -v;