Simple Script Snippets: Change CSS with PHP Date

Posted on Thursday 11th Nov 2010 by Luci Smethurst 2 Comments

A great way to keep your site up-to-date and current is to have a seasonal element – Santa at Christmas, Snow in the Winter, Pumpkins at Halloween, Bunnies at Easter..

One popular method of doing this is to have a banner area, however another way could be to use different backgrounds dependent on the time of year.

Either way – you may not have the time to go along and make the changes every few weeks/months, so here’s a way to automate it. Just make all the images and stylesheets you need ahead of time!

Here’s an example that will change your spreadsheet based on the month, with a fallback if you don’t want a special design every month:


<?php $dateCheck = date('F');

		if ($dateCheck == "March"):?>

      <link href="styles-spring.css" rel="stylesheet" type="text/css" media="screen" />

      <?php elseif ($dateCheck == "July"): ?>

		<link href="styles-summer.css" rel="stylesheet" type="text/css" media="screen" />

		<?php elseif ($dateCheck == "October"): ?>

		<link href="styles-halloween.css" rel="stylesheet" type="text/css" media="screen" />

		<?php elseif ($dateCheck == "December"): ?>

		<link href="styles-christmas.css" rel="stylesheet" type="text/css" media="screen" />

<?php else: ?>

		<link href="styles.css" rel="stylesheet" type="text/css" media="screen" />

<?php endif; ?>

And in this example the stylesheet changes based on the time (H = is hours on a 24 hour clock with leading zeros):

<?php $timeCheck = date('H'); echo $time;

		if ($timeCheck > 04 && $timeCheck <= 09):?>

		morning: <link href="styles-morning.css" rel="stylesheet" type="text/css" media="screen" />

		<?php elseif ($timeCheck > 09 && $timeCheck <= 17): ?>

		<link href="styles-day.css" rel="stylesheet" type="text/css" media="screen" />

      <?php elseif ($timeCheck > 17 && $timeCheck <= 20): ?>

		<link href="styles-evening.css" rel="stylesheet" type="text/css" media="screen" />

      <?php elseif ($timeCheck > 20 && $timeCheck <= 04): ?>

		<link href="styles-night.css" rel="stylesheet" type="text/css" media="screen" />

		<?php else: ?>

		<link href="styles.css" rel="stylesheet" type="text/css" media="screen" />

		<?php endif; ?>

About Luci Smethurst

Luci likes geeky things, green things, and chocolate. That pretty much sums her up, really.

See all of posts.

Add on Luci Smethurst Google+

Luci Smethurst's Tweet's

  • @missrachilli hold out! you can do it -strepsil it up! Go by the 3 day rule - day 1: bad; day 2: awful; day 3: (a bit) better :) Less than 1 minute ago!
  • RT @daniel_c_long: Cool Photoshop action to remove gradient banding http://t.co/XQ2lUZoj via @brdrck Less than 1 minute ago!
This entry was posted in PHP Scripts & Tutorials and tagged , . Bookmark the permalink.

2 Responses to Simple Script Snippets: Change CSS with PHP Date

  1. Jerry Lee says:
    June 5, 2011 at 9:55 pm

    Hey, I am rebuilding my site, and need to load different stylesheets on different pages. I know it can be done with php, with else and if, like you did with the time, but how do I do it by page?
    thanks

  2. James Bavington says:
    June 6, 2011 at 9:55 am

    @Jerry Lee, check out this video blog, about conditional statements: PHP Includes with Conditional Statements

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>