I have $fromdate
and $todate
variables. I want a function that can calculate the dates of each Sunday existing in between $fromdate
an
Try this
function getDateForSpecificDayBetweenDates($start, $end, $weekday = 0){
$weekdays="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday";
$arr_weekdays=split(",", $weekdays);
$weekday = $arr_weekdays[$weekday];
if(!$weekday)
die("Invalid Weekday!");
$start= strtotime("+0 day", strtotime($start) );
$end= strtotime($end);
$dateArr = array();
$friday = strtotime($weekday, $start);
while($friday <= $end)
{
$dateArr[] = date("Y-m-d", $friday);
$friday = strtotime("+1 weeks", $friday);
}
$dateArr[] = date("Y-m-d", $friday);
return $dateArr;
}
Here's how this function can be called...
$dateArr = getDateForSpecificDayBetweenDates("Today", "+1 year", 0); // 0 Sun, 1 Mon, etc.
$start
and $end
dates will take anything that strtotime()
supports.