Automatic Theme Changer with PHP

Circa 2009
Automatically update your site's CSS and logo (theme) depending on a date range. The below code allows you to specify a logo, alternate text for the logo (or a Theme Name), and a cascading style sheet by a range of days in a month.

This system CANNOT handle days that are not in the same month.

Syntax of the theme specification array:

AABBCC,D,E,F
AA - Two digit month of activity.
BB - Two digit day BEFORE event starts in month.
CC - Two digit day AFTER event ends in month.
D - Logo
E - ALT text of logo
F - Custom CSS to apply


NOTE: You must start the array at zero (0) and increment in amounts of one (1) so that the default selector can operate properly. The dates do not need to be legal. I.E. For leap year, I specified February 28 as the day BEFORE and February 30 as the day AFTER...Obviously the 30th isn't a legit date.

If you do not specify an Image, CSS, or ALT text for a theme, the code will default to the values contained in the last few lines -- This will also be the default used when there are no themes needed (i.e. The date isn't specified in the theme selector, and the highest array value does not declare any theme specifications. You'll notice in the provided theme arrays that most of them do not contain alternate CSS files -- this is because I never made one up for these events, so it will default to the one specified.

Download Original
  1. <?PHP
  2. //Themes
  3. $ThemeDate = "";
  4. $ThemeDate[0] = "102032,logoimage_halloween,It's Halloween,style-hallo";
  5. $ThemeDate[1] = "111430,logoimage_thanksgiving,It's Thanksgiving,";
  6. $ThemeDate[2] = "040002,logoimage_aprilfools,It's April Fools,";
  7. $ThemeDate[3] = "121226,logoimage_christmas,It's Christmas,";
  8. $ThemeDate[4] = "050406,logoimage_cincodemayo,It's CincoDeMayo,";
  9. $ThemeDate[5] = "041530,logoimage_easter,It's Easter,";
  10. $ThemeDate[6] = "070305,logoimage_independance,It's Independance Day,";
  11. $ThemeDate[7] = "122532,logoimage_newyears,It's New Years,";
  12. $ThemeDate[8] = "010002,logoimage_newyears,It's New Years,";
  13. $ThemeDate[9] = "021320,logoimage_racing,It's NASCAR,";
  14. $ThemeDate[10] = "031618,logoimage_stpatty,It's St. Patricks Day,";
  15. $ThemeDate[11] = "061315,logoimage_flagday,It's Flag Day,";
  16. $ThemeDate[12] = "052531,logoimage_memorial,It's Memorial Day,";
  17. $ThemeDate[13] = "090210,logoimage_labor,It's Labor Day,";
  18. $ThemeDate[14] = "022830,logoimage_leapyear,It's Leap Year,";
  19. $ThemeDate[15] = "081719,logoimage_spindown,It's Birthday,style-oldy";
  20. $ThemeDate[16] = "111012,logoimage_veteran,It's Veterans Day,";
  21.  
  22. //Last Array Item -- Forces Default if no results.
  23. $ThemeDate[count($ThemeDate)] = "000000,,,";
  24.  
  25. //$ThemeStep=2;
  26.  
  27. //Process Themes
  28. //Set Varibles for current date
  29. $ThemeCurrentDay = date("j",time());
  30. $ThemeCurrentMonth = date("n",time());
  31. for ($ThemeStep = 0; $ThemeStep < count($ThemeDate);$ThemeStep++)
  32. {
  33. $ThemeMonth = substr($ThemeDate[$ThemeStep],0,2);
  34. $ThemeStart = substr($ThemeDate[$ThemeStep],2,2);
  35. $ThemeEnd = substr($ThemeDate[$ThemeStep],4,2);
  36. if ($ThemeMonth==$ThemeCurrentMonth)
  37. {
  38. if ($ThemeCurrentDay > $ThemeStart and $ThemeCurrentDay < $ThemeEnd)
  39. {
  40. //Match found, break out of For Loop
  41. break;
  42. }
  43. }
  44. }
  45. $ThemeArray = explode(",",$ThemeDate[$ThemeStep]);
  46. $ThemeIMG = $ThemeArray[1];
  47. $ThemeALT = $ThemeArray[2];
  48. $ThemeCSS = $ThemeArray[3];
  49. //If Theme doesn't have all variables specified, or there are no active themes:
  50. if ($ThemeIMG=="") $ThemeIMG = "logoimage_default";
  51. if ($ThemeALT=="") $ThemeALT = "Welcome to our site";
  52. if ($ThemeCSS=="") $ThemeCSS = "default_css";
  53. ?>
  54.  
  55. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  56. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  57. <head>
  58. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  59. <meta name="language" content="en" />
  60. <title>YouN00b Auto Theme Switcher in PHP</title>
  61. <link type="text/css" rel="stylesheet" href="http://www.yoursite.com/path-to-css-files/<?PHP echo $ThemeCSS; ?>.css" />
  62. </head>
  63. <body>
  64. <img src="http://www.yoursite.com/path-to-images/<?PHP echo $ThemeIMG; ?>.png" alt="<?PHP echo $ThemeALT; ?>"/>
  65. </body>
  66. </html>

It would be impossible to make this easy for everybody (beginner to advanced). If you're new to PHP, ask me some questions about it. If you're advanced with PHP, you may like that you don't have to sift through tons of comments to get the code out.

This code is live, and drives one of my site's theming system. So Enjoy!

Name:

No comments yet! Be the first!