PHP Code Snippet: Automatically Updating Copyright Date

CalendarThe 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. (Image Credits: erix! )

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.

 

Can’t use PHP? You can accomplish this same task using JavaScript.

Tags:

6 Responses to “PHP Code Snippet: Automatically Updating Copyright Date”

Trackbacks

Trackback URL for this entry:
http://www.velvetblues.com/web-development-blog/php-code-snippet-automatically-updating-copyright-date/trackback/

Leave a Reply

CommentLuv Enabled

Want us to work on your project?

Contact us today for a quote. Click here to submit details regarding your project.

If you are making a general inquiry, send an email to info@velvetblues.com