Calculating Floating Holidays in PHP
Since I’ve spent so much time figuring out the best method for calculating floating holidays in PHP, I’ve decided to share this little function with everyone. I found the best calculation method and mixed it in with what I thought was a little more useful. Just feed in the needed parameters and you’re good to go!
-
function ordinal_day($ord, $day, $month, $year) {
-
$earliestDate = 1 + 7 * ($ord - 1);
-
-
if ($targetDay == $weekday) {
-
$offset = 0;
-
} else {
-
if ($targetDay < $weekday) {
-
$offset = $targetDay + (7 - $weekday);
-
} else {
-
$offset = ($targetDay + (7 - $weekday)) - 7;
-
}
-
}
-
-
// Calculate the actual date of the holiday
-
-
return $holidayDate;
-
}
The parameters:
- $ord - The number of days. Ex. “The fourth Thursday in November” (Thanksgiving)
- $day - The short abbreviation of the day of the week. Ex. “Thu”
- $month - The month in number form. Ex. “11″
- $year - The year in four-digit form. Ex. 2007
Filed under: Web Developing on November 14th, 2007
You’re such a smartie! :)