I have the following code that creates a list of dates between a range. If I supply a day of the week it will get all dates for thoes dates in the range. If I dont it will get all days.
My problem is that from the 24th of october to the 29th it returns an extra day. I noticed the before when I added the extra code to get the dates of a day of the week.
The problem line I think is array_push($aryRange,date(‘Y-m-d’,$iDateFrom)); // first entry
function createDateRangeArray($strDateFrom,$strDateTo,$dateOfWeek=null) {
// takes two dates formatted as YYYY-MM-DD and creates an
// inclusive array of the dates between the from and to dates.
// could test validity of dates here but I'm already doing
// that in the main script
$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) {
if(!isset($dateOfWeek)) {
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));
}
} else {
while ($iDateFrom<$iDateTo) {
if(date('w',$iDateFrom)==$dateOfWeek)
array_push($aryRange,date('Y-m-d',$iDateFrom));
$iDateFrom+=86400; // add 24 hours
}
}
}
if(count($aryRange)<1){
return false;
} else{
return $aryRange;
}
}
Wouldn’t it be simpler to use the strtotime() function?
http://php.net/manual/en/function.strtotime.php