How do I check if a date is a holiday?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
funphp
Forum Newbie
Posts: 4
Joined: Sun Jan 18, 2009 12:38 am

How do I check if a date is a holiday?

Post 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.
Last edited by funphp on Sun Jan 18, 2009 1:48 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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";
}
funphp
Forum Newbie
Posts: 4
Joined: Sun Jan 18, 2009 12:38 am

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

Post by funphp »

what about observed holidays? Like Labor Day or thanksgiving. I guess the timestamp requirement is for those days?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Well yeah. Can't check for holidays you don't know about, right?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post 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.
funphp
Forum Newbie
Posts: 4
Joined: Sun Jan 18, 2009 12:38 am

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

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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
Post Reply