Tidbit Tuesday on PHP: Holiday Notices for Business Sites

This is a handy script for managing an out of office message on your website.  I find this very useful on business websites when you’re tired of making that one little change to let people know when the office will be shut down.  With about 15 minutes of extra work, you only need to do it once a year.

Let’s say for instance we’re doing all of the major U.S. Holidays in 2010, observing weekend Holidays with a day off on either side of the weekend.  This means first we need to determine what those dates are.  I just happen to have looked up the six most common, listed below:

  • New Year’s Eve – January 1st
  • Memorial Day – May 31st
  • Independence Day – July 5th (July 4th is Sunday)
  • Labor Day – September 6th
  • Thanksgiving – November 25th and 26th
  • Christmas – December 24th (December 25th is a Saturday)

Now that we know our dates, we can build them into an array:

$date_array = array(
   "New Yearís Eve" =>
      array("leaving" => "2010-12-31", "returning" => "2011-01-03"),
   "Memorial Day" =>
      array("leaving" => "2010-05-31", "returning" => "2010-06-01"),
   "Independence Day" => // July 4th is a Sunday
      array("leaving" => "2010-07-05", "returning" => "2010-07-06"),
   "Labor Day" =>
      array("leaving" => "2010-09-06", "returning" => "2010-09-07"),
   "Thanksgiving" =>
      array("leaving" => "2010-11-025", "returning" => "2010-11-029"),
   "Christmas" => // December 25th is a Saturday
      array("leaving" => "2010-12-24", "returning" => "2010-12-27"),
 );

The date array can easily be stored in a database or be made up of more complicated functions that would calculate the holidays across multiple years with some basic logic.  Maybe we’ll cover the latter part in another post some day.

Now we need to build the function itself.  This function will be run every time the page loads.  You can even include the function in a separate file to be included on multiple pages:

function holiday_message($date, $date_array) {
   $date = strtotime($date); // Reformat the date so we can do math on it.
   foreach($date_array as $key => $value) {
      $leaving = strtotime($value['leaving']);
      $returning = strtotime($value['returning']);
      $early_warning = $leaving - 86400 * 7;
      // If the date is between (7 days before) leaving and returning
         if($date > $early_warning && $date < $returning) {
            echo
               "We will be observing ".$key." from ".date("Y-m-d", $leaving).
               " until ". date("Y-m-d", $returning).".
               When we return we will be more than happy to assist you.";
         }
   }
}

Now we just need to execute this function anywhere on the page:

holiday_message(date("Y-m-d H:i:s"),$date_array);

This code can be customized in numerous different ways, so the the message is more formal, or works for your specific situation.  You can also change the way days are stored or calculated, like the example above.  This is a core to get you started.  Where can you go from here?

A review of 2008.

Here are some of the more popular blog posts that I made in 2008:

Hands down, the most popular post I made was How to Make Fire in Photoshop.

In March, I made a controversial post about Macs, PCs, and Linux.  I should probably do a follow up on this.

I found a solution to my Outlook / Google Calendar synchronization issues.  Actually Google found a solution.  Thanks Google.

I listed my top ten free applications.  I plan to do another version of this post in 2009.

I made a few submissions to shirt.woot derbies, but never won.  I have no problem admitting that the competition over there is pretty stiff, and the technical ability of some of the artists surpasses my own by quite a bit.

In December, I started doing contests for shirt.woot shirts.  I’ll probably change this up in the future to offer other things.  This just seemed like a great place to start.

I know I also did some things wrong.

By far the biggest thing I regret doing in 2008 was taking a break from blogging from May to September.  I think that really hurt my readership, and I’m going to try and refrain from doing that at all this year.  My blog also lacked focus.  I’m still having trouble breaking my topics apart.  Right now I think my lack of focus leaves readers wondering what I’m talking about half the time.  I’m really passionate about Woot, but a lot of internet marketers probably don’t care, and when I’m talking about internet marketing and programming, Wooters probably get bored.  Interesting perplexity.  I will try to address this in 2009 as well.

I have some pretty exciting plans for 2009 that I will follow up on later.

Google Calendar / Outlook Sync

I’ve been looking for a way to sync my Google / Outlook 2007 calendar for a bit (2 ways), and the built in Outlook features allow you to add an internet calendar, but it needs to be public and doesn’t show in your calendar view on your email page. it’s also one way. I found a solution that worked, but it was sort of a hack job, so I didn’t really stand behind it.

THEN! I noticed “Sync with Microsoft Outlook™ calendarNew!” in the top right area of my calendar. It took me about 2 seconds to decide to click the link and download the application. This thing is great! If you can’t see the URL on your Google Calendar, go here:

http://www.google.com…answer.py?answer=89955

Let me know if you have any issues in the comments.  I’m interested in what they might be.

-JKT

Update: If you’re using Windows Vista and Outlook 2007, for some reason, sometimes the application cannot connect and sync for the first time if you have Outlook open. Just close Outlook and sync for the first time. Voila!