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.

WordPress: open external links in a new window

When I write my posts in WordPress, I sometimes forget to set target="_blank" for the links I want to open in a new tab (or window, depending on the browser’s configuration). I could go back and edit my posts in WordPress, but it would be a tedious process. Since I want only external links to open in a new tab, I decided to add jQuery to select all links pointing to external sites and then set the target attribute automatically. I modified the header.php file for my WordPress theme to do just that:

Essentially, this snippet of code identifies all external links by:

  1. selecting all the anchor tags in the document
  2. removing all links that don’t begin with http, https or ftp, as they are assumed to be relative links
  3. removing all links that, while absolute, point to the same domain

Once the external links are identified, the target attribute is set to _blank.