Page 1 of 1

How do I check if a date is a holiday?

Posted: Sun Jan 18, 2009 12:47 am
by funphp
Let say we have three holidays in a year, that's New Year Day (01/01), Independence Day (07/04), and Christmas Day (12/25). If a user enters a date, I want to check whether the input date is a holiday or not. Note that the dates are written in the United States format (month/day). I need to use the timestamp to compare the dates.

Re: How do I check if a date is a holiday?

Posted: Sun Jan 18, 2009 1:08 am
by requinix
Maybe you're over-thinking it?

Code: Select all

$date = "12/25";
 
if ($date == "01/01" ||
    $date == "07/04" ||
    $date == "12/25") {
    echo "$date is a holiday";
} else {
    echo "$date is not a holiday";
}

Re: How do I check if a date is a holiday?

Posted: Sun Jan 18, 2009 1:15 am
by funphp
what about observed holidays? Like Labor Day or thanksgiving. I guess the timestamp requirement is for those days?

Re: How do I check if a date is a holiday?

Posted: Sun Jan 18, 2009 1:31 am
by requinix
Well yeah. Can't check for holidays you don't know about, right?

Re: How do I check if a date is a holiday?

Posted: Sun Jan 18, 2009 1:40 am
by RobertGonzalez
What constitutes a holiday for you? Are they going to be in a database, an array, a file? Checking against a known date is not too hard to do but you need to know which dates are holidays and what the date you want to compare it to is.

Re: How do I check if a date is a holiday?

Posted: Sun Jan 18, 2009 2:00 am
by funphp
There's no database involved here, just PHP code.

OK, let's say the following are holidays:

New Year Day (01/01)
Easter Good Friday
Memorial Day
Independence Day (07/04)
Labor Day
Thanksgiving Day
Christmas Day (12/25)

So for the fixed holidays it's not hard to get, but for the observed holidays, it depends on the year the user enters. How do I get that?

Re: How do I check if a date is a holiday?

Posted: Sun Jan 18, 2009 3:27 am
by requinix
Oh, I see what you're asking about.

Watch this:

Code: Select all

echo date("Y-m-d", strtotime("October 31st 2008, next Thursday, +3 weeks"));
The commas aren't necessary but it helps you think about that in the three parts it really is. However the year is important: without it strtotime will fail and you'll get the wrong answer.

There's no simple way to get the first Thursday of a month directly, but you can figure out the next Thursday after a certain date. However if you chose that "certain date" to be the last day of the previous month... Add three weeks and you have the fourth Thursday of November.

Holiday rules, taken from Wikipedia:
Easter: it's complicated. Figure out when Easter was and go back to the previous Friday ("previous" works with strtotime).
Memorial Day: last Monday of May (hint: use June 1st and "previous")
Labor Day: first Monday of September
Thanksgiving: fourth Thursday of November