Create a Christmas Class and Condition with PHP

Posted Saturday 8th Dec 2012 by Rob Bavington

For this week’s video blog I wanted to share some simple PHP code that’s really handy for dynamically adjusting your website during the Christmas period. Whether you’re looking to add a Christmas background graphic or a temporary delivery message, this functionality can be used to exactly that with the added advantage of appearing and disappearing within a specified timescale each year.

In the video embedded above, I give a quick walk through of the code, and explain how to use it to achieve either of these two results. Once you’re ready to start using the code, simply follow the simple steps below:

Add the following code to your template

Within your document template or include, run the following PHP so that we can reference the $itschristmas variable anywhere on the website:

<?php
$curr_ts = time();
$curr_year = date("Y");

if(date('m') == '01') { // If we're in January...
    $christmas_from_year = $curr_year - 1; 
    $christmas_to_year = $curr_year; 
} else {
    $christmas_from_year = $curr_year; 
    $christmas_to_year = $curr_year+1;  
}
$christmas = array();
$christmas['from'] = strtotime("01-12-".$christmas_from_year." 23:59");
$christmas['to'] = strtotime("01-01-".$christmas_to_year." 23:59");
$itschristmas = false;
if($curr_ts > $christmas['from'] && $curr_ts < $christmas['to']){
	$itschristmas = true;
}
?> 



<body class="<?php if($itschristmas){ echo "christmas"; } ?>">



<?php if($curr_ts > $christmas['from'] && $curr_ts < $christmas['to']) { ?>
<?php } ?>

Add in your Christmas Message or information

The code above allows for either a class of “christmas” to be added to the body or to add in a content area that only appears within your specified date range.

It really is that simple, I hope you have found this tutorial useful and please let us know about any ways that you have added a bit of festivity to your website.

From everyone here at Creare, we would like to wish you a very Merry Christmas and a Happy New Year!

By Rob Bavington

I am a great lover of the online design world and hold a passion for great passion for typography. I play the guitar and enjoy a spot of fishing on a relaxing Sunday afternoon

See all of posts.

Connect with Rob Bavington on Google+

Rob Bavington's latest tweets