How to calculate the list of dates between two dates

In this post i want to explain how to calculate the list of dates between two dates using php code. Just we have to give the starting date and ending date and this function will calculate the dates including leap year and monthly days … It will be mainly useful in the calculate the statistics or register users between given dates.

How to calculate the list of dates between two dates | Anil Labs

How to calculate the list of dates between two dates | Anil Labs

 

PHP Code:

 

<?php
$fromDate = "02/14/1983"; // month / day / year
$toDate = "06/07/2011";  // month / day / year

$dateMonthYearArr = array();
$fromDateSTR = strtotime($fromDate);
$toDateSTR = strtotime($toDate);

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


print_r($dateMonthYearArr);


?>

Hope this will be useful.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online

Contact : [email protected]

You may also like...

4 Responses

  1. Very nice tip. Blog template menus so cool

  2. Abu says:

    its is the list from to two dates in array..
    how to calculate the no.of.days??

  1. October 29, 2011

    […] choose days, then it is calculate the difference between from date and to date using previous post How to calculate the list of dates between two dates . After calculate the days we can easily get the list of records from database and draw the your […]

Leave a Reply

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