PHP PHP Code

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.

5 thoughts on “How to calculate the list of dates between two dates”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.