PHP function to find dates between two dates
Here i am going to write a function for finding dates between two Dates using PHP. It will output all the days between two given dates as a array. The main feature of this is,
->It will output all days in array format
You can use this function to find number of days between given two dates as below.
Displaying all days:
To display all days, just write the below code
<?php
<?php
var_dump(createDateRangeArray("2011-02-02","2013-02-02"));
?> Displaying Number of days:
To display number of days between these two dates just use the below code
count(createDateRange("2011-02-02","2013-02-02"));
?>
Main Function:
Here is the function to do all the above required things. Just copy it in your PHP file and display dates as i specified above.
<?php
function createDateRange($strDateFrom,$strDateTo) {
//two dates formatted as YYYY-MM-DD and creates an
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom) {
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo) {
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
} } return $aryRange;
} ?>
ReplyDeleteWonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
PHP Developer Software