How to Automate Website Copyright Dates using PHP

Have you ever updated your website after the end of the current year and then realized that all of your copyright dates on your webpages were a year behind? You HAVE updated your website at LEAST annually, right?

We will make a couple of assumptions here, even though making assumptions can be dangerous!

Firstly, we assume that you have a php configuration or definition file as the very first item loading on all of your php web pages. This php file would contain either your function definitions or would require various php function libraries to load. If you are using php at all, this is pretty basic. We will have more details on specifics later.

Secondly, we will assume that somewhere on your webpages you have copyright data such as "Copyright © 2004 Your Name Your Company Name All right reserved, Your Location", or something similar.

Ok, so we have the basics in place, now we just want to automagically update that pesky copyright date so that it always shows the correct year, as long as our webserver knows the correct date, which in itself is sometimes a challenge.

First, we need a function in php to accomplish the update. I called mine simply echo_current_year() for simplicity. It looks like this:


// This function copyright 2007 Jim Smith Ideas4You.Com
function echo_current_year()
{
$today=getdate();
$year=$today[year];
print $year;
return;
}

It has no variables passed to or from it, it just works. First, it uses php's built-in getdate() function to get the current datestamp from the server, then it retrieves the four digit year from that datestamp, and lastly, prints that four digit year on the screen. What could be simpler?

Using the example from above, we would replace
Copyright © 2004 Your Name
with this:

Copyright © <?php echo_current_year() ?> Your Name
This would result in your new copyright display like this:
Copyright © 2010 Your Name

You can see that simply placing <?php echo_current_year() ?> everywhere in your php page that you want the current year displayed accurately beats the heck out of constantly having to change the dates manually. In fact, as you can see from the footer below, since our site is a work in progress, it normally displays 1997-2010 for the copyright dates to show beginning and ending years.

Feel free to use the function above for personal OR commerical use, just don't sell it, and please keep any copyright info in code comments in place.