Auto Changing Holiday Logos using PHP
I’ve noticed more and more sites changing their site’s logo during holidays. Some even change the theme of the entire website. Sure, you could manually do this, but there is no need to do this when you can use scripting to do it.I’ve noticed more and more sites changing their site’s logo during holidays. Some even change the theme of the entire website. Sure, you could manually do this, but there is no need to do this when you can use scripting to do it. So let’s learn how to change your logo dynamically through scripting so that when it is Christmas, for example, you would have a Christmas-oriented logo displayed on your website.
You’ll need to make on your own (or with the help of your favorite designer) a logo made for each holiday upon which you want to change your logo, but I’m going to show you the scripting part so that can make this happen for you behind the scenes automatically. For this I’m going to use PHP scripting.
It’s just a matter of time – server time.
When surfers come to your website they very possibly could be in a different time zone than where your server is located. Let’s say your server is located in New York, USA and I’m surfing from Seattle, USA. There is a 3 hours difference less than what the server time is. All scripts inherently work off the server time unless you create what’s known as an offset. If you know what the surfer’s time zone is, say because you asked for it in advance, you can easily put an offset into the script and then make sure that if Christmas day was in fact yesterday for the surfer that you were showing them the default (non-holiday) logo.
I will save (for another day) grabbing surfer time and comparing to server time to automatically create an offset but you should realize that this is one aspect of the script herein which in a sense remains a design issue.
In the interest of brevity and simplicity I am not providing the solution to this issue in this particular column. In layman’s terms: the worst case is that you won’t be any more than 23 hours and 59 minutes (23:59) off on any surfer that comes to your page. I’m sure they can live with seeing a Christmas logo up to 23:59 earlier (than they actually should) and up to 23:59 after they should. If enough people write me and request that I show you how to create this offset so there would be real-time holiday logo automation anywhere in the world for every surfer, I will be happy to do so in a future article.
STEP 1. Define the path to where the logo images are stored and dates in the format of:
date of event | alt description | logo filename (remember UNIX is case seNsItiVe)
The pipe symbol delineates the data.
<?
$image_path = \’http://www.yourdomain.com/images\’; // you can use relative paths instead if you want
$toshow = \"default_logo.jpg\"; // when no dates match
$alt = \"My default logo alt tag description\"; // when no dates match
$events = array(\"1-1-2002|New Year\’s Day |newyears_logo.jpg\",
\"2-2-2002|Groundhog Day |groundhog_logo.jpg\",
\"2-14-2002|Valentine\’s Day |valentines_logo.jpg\",
\"3-17-2002|St Patrick\’s Day |stpattys_logo.jpg\",
\"4-1-2002|April Fool\’s Day |aprilfools_logo.jpg\",
\"4-15-2002|Easter |easter_logo.jpg\",
\"5-5-2002|Cinco de Mayo |cinco_logo.jpg\",
\"5-13-2002|Mother\’s Day |mothers_logo.jpg\",
\"5-28-2002|Memorial Day |memorial.jpg\",
\"6-17-2002|Father\’s Day |fathersday_logo.jpg\",
\"7-1-2002|Canada Day |canadaday_logo.jpg\",
\"7-4-2002|4th of July |july4th_logo.jpg\",
\"9-3-2002|Labor Day |labor_logo.jpg\",
\"10-31-2002|Halloween |halloween_logo.jpg\",
\"11-22-2002|Thanksgiving |thanksgiving_logo.jpg\",
\"12-25-2001|Christmas |christmas_logo.jpg\"
);
STEP 2. Get the current server date using the PHP getdate() function. The getdate() function gives us the following associative array elements:
hours, mday, minutes, mon, month, seconds, wday, weekday, yday, year, 0
We’ll only need the date characteristics: mon, mday, year which would be the date in the format (12 25 2001) which is Christmas day (change the year based on the year you read this article). $target below will equal 12-25-2001 which would match the Christmas day date in the $events array in Step 1.
$datearray = getdate();
$target = $datearray[\"mon\"] . \’-\’ . $datearray[\"mday\"] . \’-\’ . $datearray[\"year\"];
STEP 3. Split the array by the pipe delimiter and then look for a matching date. If there is no match $toshow still equals the default logo with the default $alt tag description, but if there is a match then we break the loop so we don’t cycle through the rest of the array needlessly. This is the meat of the routine and closes out the code.
$sizeof = count($events);
$i = 0;
while($i<$sizeof) {
$esplit = explode(\"|\", $events[$i]);
if($target == $esplit[0]) {
$toshow = $esplit[2];
$alt= $esplit[1];
break;
}
$i++;
}
?>
STEP 4. Insert the logo in the html body portion of your webpage at the desired place escaping briefly to PHP to grab and print the code.
<BODY>
<?
print(\"< IMG SRC=\"$image_path/$toshow\" alt=\"$alt\" border=0 width=468 height=80\">\");
?>
STEP 5. Test the code by changing the date of Father’s Day (or some other holiday) in the $events array to today’s server date and then call your webpage from your browser. You should see the logo for Father’s Day. Change the date back and you should see your default logo. Now all you need to do is change the dates where appropriate inside the $events array and you’re all set to have your logo change every time there’s a new holiday 🙂
Have a great holiday, whichever one is close to the date you read this article!
TDavid is co-owner, programmer and webmaster for several sites devoted to programming including his own http://www.tdscripts.com/. He has done custom programming in various programming languages for companies all over the world. Every Friday at 2pm PST you can catch his weekly radio show dedicated to the technical side of webmastering and programming at http://www.scriptschool.com/radio.