Automated Style Switcher

With the Christmas period upon us, I got to thinking… wouldn’t it be cool to be able to change the style of your site automatically depending on the time of the year?

Well with PHP it’s easy!

$today = date(′d/m′);
echo "<style type="text/css">";
echo "/*<![CDATA[*/";
if  ($today == ′25/12′)  {
echo "@import url(css/xmas_style.css);";
}  elseif ($today == ′01/01′)  {
echo "@import url(css/newyears_style.css);";
} else {
echo "@import url(css/style.css);";
echo "/*]]>*/";
echo "</style>";

First you assign a value to the $today variable using the date() function.

Then you echo out the beginning of the style tag. Then using a simple if…elseif…else statement you check if the value of the $today variable is equal to a certain date. If so, import the appropriate stylesheet. Finally you need to echo out the end of the style tag.

The code above needs to be placed inside the <head></head> tags in order to work. Also, this will only work properly on sites that have separated presentation from content.

Of course the code above only works on a day-to-day basis. If, for example, you wanted to have an xmas stylesheet be used for the entire month of December you would need to edit the code to check if the day falls between 01/12 – 31/12, but I’ll leave that up to you to figure out :)