PHP: Get date for nth occurrence of weekday
I looked quickly and did not see any code out there to determine the nth occurrence of a specific day of the week in a month. For example Thanksgiving is the 4th Thursday in November. So I designed the following
<?php
$year = 2012;
//Pick your year
$interval = 4;
//Week number, in this case the 4th week
$dayofweek = 4;
//Day of week 0=Sun, 1=Mon. . .
$month = 11;
//Month 1=Jan, 2=Feb
$firstday = jddayofweek(gregoriantojd($month, 1, $year), 0);
if($firstday <= $dayofweek){
$date = (($interval * 7)-6) + ($dayofweek - $firstday);
}else{
$date = (($interval * 7)-1) + (6 - $firstday);
}
echo date("n/j/Y", mktime(0, 0, 0, $month, $date, $year));
?>
It looks a little messy, but it works perfectly.
UPDATE:
So apparently as bad as this looks it works too:
