Calculating U.S. Federal Holidays with PHP

A while ago, I had written a PHP class that would calculate all the U.S. Federal Holidays for a given year. I recently had a chance to take a second look at the code and discovered a few cases where the dates returned by my function weren’t matching the holidays provided by the U.S. Office of Personnel Management. In particular, I had failed to account for those fixed holidays (e.g. Independence Day, on July 4) that fall on a Saturday or Sunday: in those cases, the preceding Friday or following Monday, respectively, will be considered a Federal Holiday. As far as I can tell, the Federal Holidays haven’t changed since 1968, but the U.S. Office of Personnel Management’s web site doesn’t provide a calendar prior to 1997, so I decided to put the same limitation in my code:

If the class is instantiated without any parameter, the current year will be implied; if a year is passed to the constructor, it will be checked whether it is an integer great than 1996 (and throw an exception if not). The class has only one public method, get_list(), which will return an array of holidays: each holiday is represented as an associative array containing two elements, name (e.g., Christmas) and timestamp. The following example will show a table with the US Federal Holidays for the current year:

The result for 2013 should be like this:

New Year’s Day January 1, 2013
Birthday of Martin Luther King, Jr. January 21, 2013
Wasthington’s Birthday February 18, 2013
Memorial Day May 27, 2013
Independence day July 4, 2013
Labor Day September 2, 2013
Columbus Day October 14, 2013
Veteran’s Day November 11, 2013
Thanksgiving Day November 28, 2013
Christmas December 25, 2013
Today (November 17, 2013) is not a holiday.

2 Comments

  1. Marion Dorsett February 16, 2017 at 10:07 pm

    I found your class, and it was very useful, however, it changes the date_default_timezone_set() for the application which is not good.

    I rewrote the __construct()or method, to use the PHP DateTime and DateTimezone classes instead.

    If you’re interested I can email the revised code to you, or if you have it on GitHub, I can update it there and you can merge the changes.

  2. Aaron Dinin February 18, 2017 at 7:48 pm

    This was a fantastic bit of code. Since nobody else has commented on it, I figured I would just to say “thank you!”

Leave a comment

Your email address will not be published. Required fields are marked *