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.