I’ve been using this function to build an array of dates from a “FromDate” to a “ToDate” and just noticed the results are not always correct. I’m hoping someone can point out why or a more reliable way to build this array. You’ll notice some commented out sample dates and the result. Thank you for your expertise.
<?php
function createDateRangeArray($strDateFrom,$strDateTo){
$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;
}
//Incorrect
//$FromDate = "2013-11-01";
//$ToDate = "2013-11-30";
//Correct
//$FromDate = "2013-11-01";
//$ToDate = "2013-11-15";
//Correct
//$FromDate = "2013-12-01";
//$ToDate = "2013-12-30";
//Correct
//$FromDate = "2013-11-15";
//$ToDate = "2013-12-15";
//Correct
//$FromDate = "2013-11-15";
//$ToDate = "2013-12-30";
//Incorrect
$FromDate = "2013-11-01";
$ToDate = "2013-12-30";
$dates_array = createDateRangeArray($FromDate,$ToDate);
echo "$FromDate<br />$ToDate<br />";
echo "<pre>";
print_r($dates_array);
echo "</pre>";
?>