Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Looking for a solution: Holiday Calculation

    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).

    Examples:

    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.

  2. #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,890
    Thanks
    8
    Thanked 1,131 Times in 1,122 Posts
    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.

    http://www.google.com/search?hl=en&q...=Google+Search

  3. #3
    New Coder
    Join Date
    Dec 2005
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  4. #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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

  5. #5
    New Coder
    Join Date
    Dec 2005
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Holiday Calculation: My Solution

    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:
    id
    holiday
    occurrence
    date_holiday
    date_start
    date_end

    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');
    etc.

    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.

  6. #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    He gives you the source code. It would've been easiest/efficient to just use that.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •