List Date Difference

Some of you might have come across the requirement for displaying all the dates between give two dates, especially for drop down listing of dates.

I have developed a sample code for getting all date ocurrences between give two dates.


<?php

$fromDate = "01/01/2009"; //format mm/dd/yyyy
$toDate = "01/25/2010"; //format mm/dd/yyyy

$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);

for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date("d-m-Y",$currentDateTS); //format d-m-Y
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}

echo  "<pre>";
print_r($dateMonthYearArr);
echo "</pre>";
?>

You can change the date and modify the code as per you requirement.


Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top