Saturday, September 24, 2011

Automatically Updating Copyright Date-PHP Code Snippet


The New Year is right around the corner, which means that there are all sorts of things that you are probably doing to get ready for it. Well here’s one more thing: You will need to update the copyright date in the footer of your website

The typical approach to updating the date is to go in and manually change the date every year. And while this isn’t especially difficult or time consuming, it can be done more efficiently.
The code sample below, written in PHP, will update your Copyright date every year without requiring you to do anything to it ever again. So when you go in to update your website in a week, be sure to add this code snippet to the footers of your pages. (Note: This can only be added to PHP pages.)

&copy; Copyright <?php
$then = 2007;
$now = date('Y');
if ($then == $now)
echo $now;
else
echo "$then - $now"; ?>



Be sure to edit $then and set it to the year that your site was launched. And that’s all. As you can see the final year is automatically calculated using PHP’s date() function. For young sites, this code has a little bit of special functionality; It  compares the two years and only outputs a range if the first year is different from the last year.
A simpler version of the code is shown below.

&copy; Copyright 2007 - <?php echo date('Y'); ?>


What does it look like? Well, in 2009, this code would output something like this:
© Copyright 2007 – 2009.

No comments:

Post a Comment