View Full Version : Looking for a solution: Holiday Calculation

03-21-2007, 11:39 AM
I have a web site where people submit events. They enter a date for their event and then it goes on to a second form where they enter all the details.

Upon reaching that second screen, I would like to automatically fill in the "holiday" field with the appropriate value (if there is a match).


Date entered: 2007-05-27
Holiday returned: Memorial Day Weekend

Date entered: 2007-05-25
Holiday returned: Memorial Day Weekend

Date entered: 2007-05-20
Holiday returned: <nothing>

The idea is to match the date entered with a range of dates around a holiday. This may be complex to achieve as not all holidays fall on a Monday (like New Year's, Christmas, St. Patrick, etc...).

I don't know if this clear, let me know, I'm open to suggestions.

Thank you.

03-21-2007, 01:59 PM
I think the hardest part here is the date format.
How you do force them to enter the date in a particular way?
2007-02-25, 02-25-2007, 2/25/2007?

I think a date validator in Javascript or AJAX directly on the
place where they enter their date would be the best method.


03-21-2007, 02:36 PM
Thanks for the concern but this is already taken care of via a JS date picker that formats the selected date to whatever is needed, in this case the mysql format: YYYY-MM-DD.

03-21-2007, 03:49 PM
I think the hardest part here is the date format.
How you do force them to enter the date in a particular way?

There are many ways to handle date formats. Using Javascript, strtotime() and various other code this is no problem at all. In fact the easiest, especially when using a calendar picker.

The hardest part is that holidays do not happen on pre-set dates. There is some logic involved.

I don't think I've seen any standard PHP function that would help you with this.

Why not code it yourself?

Create a function that takes a date, then take those dates, and for every US holiday see if there is a match. If so, return an array of holidays (array of strings).

That is how I would do it anyway. You could also look for an existing implementation. But look at it this way, with an hour of work you'll know exactly when each holiday is ;)

03-21-2007, 09:32 PM
So here's what I've done. It's not 100% automatic as I wanted first, but I'm pretty much set until 2020 with just a now and then manual update.

My Db contains the following fields:

I used Dan Kaplan's Holiday Calculation http://abledesign.com/programs/holiday.php and modified the output so it looks like this:

INSERT INTO holidays (holiday, occurrence, date_holiday) VALUES ('New Year\'s Day', 'First Day of the Year', '2020-01-01');
INSERT INTO holidays (holiday, occurrence, date_holiday) VALUES ('Martin Luther King Day', 'Third Monday in January', '2020-01-20');

I generated the output until 2020 and copied and pasted it into my Db.

Then I used DATE_ADD and DATE_SUB to fill the date_start and date_end fields.

For instance, for the Holidays falling on a Monday (Memorial Day, President's Day, etc.), I subtract 3 days to get the date_start on the Friday and copy the date_holiday to date_end:

UPDATE holidays SET date_start = DATE_SUB(date_holiday, INTERVAL 3 DAY), date_end=date_holiday WHERE holiday='President\'s Day'

For the other holidays falling on a fixed date, I edit the records manually.

Then when people submit an event, I can do a look up on the date and see if it matches a holiday date range and insert the corresponding holiday into the record.

I hope this helps someone else.

03-21-2007, 10:01 PM
He gives you the source code. It would've been easiest/efficient to just use that.