Get a List of Dates Between Two Dates with PHP

October 3rd, 2010 - Posted by Steve Marks to PHP, Web Development.

Get a list of dates between two dates using PHP

When building an events calendar recently I needed to obtain all the dates between two dates. For others looking to do the same thing I used the code below to achieve this. Simply change the $date_from and $date_to variables to get it working for you.

// Specify the start date. This date can be any English textual format
$date_from = "2010-02-03"; 
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp

// Specify the end date. This date can be any English textual format
$date_to = "2010-09-10";
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp

// Loop from the start date to end date and output all dates inbetween
for ($i=$date_from; $i<=$date_to; $i+=86400) {
	echo date("Y-m-d", $i).'<br />';
Tags: ,
This entry was posted on Sunday, October 3rd, 2010 at 10:55 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (8)
  1. kris says:

    You have to be careful here as there’s such thing
    as daylight saving tame practice.
    your dates will be screwed up if you just add a number of seconds around these dates where change occurs

  2. MarkaHolic says:

    Thanks.. its really works :D

  3. adarsh says:

    Thanks :) You saved my time bro

  4. nandhu says:


  5. satheeshkumar says:


  6. @Mohsin – Thanks. Have updated code.

  7. Mohsin Rafique says:

    An infinite loop start at your script when execute it.

  8. Dennis says:

    Thanks for the code, just what I was looking for.
    (There is a small error, you increase $currentDateTS instead of $i in the for, so there is an infinitive loop.)