Generate a list of dates between two dates in PHP

If you want to create a list of dates between two different Unix time stamps, here is how to do it:

$starting_unix_timestamp = 1000000000;
$ending_unix_timestamp = time();


$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();
for ($i=1; $i<$number_of_days_between; $i++){
//generate complete list of days between dates
$starting_date->add(new DateInterval('P1D'));


Compatible with (PHP 5 >= 5.2.0)
to check your PHP version run
or if you are running the below script from the command line
php -v;

note: php -v does not get the version of your php that you are running as an ISAPI module on the web server.

comments powered by Disqus