How do I check if a date is a holiday?
Moderator: General Moderators
How do I check if a date is a holiday?
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.
Re: How do I check if a date is a holiday?
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?
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?
Well yeah. Can't check for holidays you don't know about, right?
- 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?
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?
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?
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?
Oh, I see what you're asking about.
Watch this:
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
Watch this:
Code: Select all
echo date("Y-m-d", strtotime("October 31st 2008, next Thursday, +3 weeks"));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