You could set up an array indexed by date, and the value could be e.g., the name of the holiday, or even something more complex.
$holidays = array(
'12 25' => 'christmas',
'11 27' => 'thanksgiving',
'07 04' => '4th of July',
'01 01' => 'new years',
);
$date=date('m d', strtotime($date));
if(key_exists($date))
{...whatever is appropriate
}
$holidays of course doesn't need to be hardcoded, or at least, not built right into the script; it could be in a separate file that is included, or it could be drawn from a database, or it could actually be generated on the fly - starting with the statutories, and then adding "days off" that are scheduled on an ad-hoc basis.
"Whatever is appropriate" in this case is to return the previous date. Ah, but what if that's a holiday as well? Perhaps it should return checkholiday(date('Y-m-d', strtotime("$date -1 day")); instead 🙂- no, really, that would work.
function checkholiday($date, $holidays)
{
// $date will be in YYYY-MM-DD format
// if date falls on a holiday, then date is moved back to the first non-holiday
$md = date("m d",strtotime($date));
if(array_key_exists($md, $holidays)
return checkholiday(date("Y-m-d",strtotime("$date -1 day")), $holidays);
return $date;
}
It would be a bit on the slow side if holidays stretched on for a couple of weeks and you happened to ask for a day near the end (because of the repeated use of date() and strtotime() but since multi-day holidays are less common, that won't happen so much, and putting together a loop in the function that would keep checking back and back and back until it finds a non-holiday would probably involve more work than it saves.
Later, this passing of $holidays around might get tiring, and you could look at putting together a calendar class in which $holidays becomes a property and checkholiday becomes a method.