How to calculate the list of dates between two dates

in PHP, PHP Code / 4 Comments

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.

With more than 9 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 : php.anil@gmail.com

Discussion 4 Comments

  1. Abu

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

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>