Thursday, October 8, 2009

Auto Update Your WebsiteS Copyright Notice Each Year With PHP

If you are like me the years do sometimes just slip by. Visit family you don't see often and you know what I am talking about...the little toddlers and teenagers, that kind of stuff.

But this isn't about philosophy or self help. We have a much easier problem to fix. The dates on your copyright at the bottom of your page.

All it is going to take is just a super simple little script, but in my opinion it is useful and makes a real difference in how professional a website appears to be.

I am sure you see it often. Small business or personal home pages, very often, but sometimes you even find the website of quiet large companies with outdated copyright notices. - Example 1 - and - Example 2 -

Okay, if you have php installed on your server, this is going to be ridiculously easy, but so useful down the line. I will just cover this in a quick manner:

First off, whatever your html page, you need to save the page as a .php page instead of html. If you have links to the page already, make sure that you update them to .php instead of .html or .htm.

Changing that does nothing else to your page as it is already, it just means that you can now embed the php scripts into the page.

Okay, here is the script -

<?php

$thisYr = date("Y");
if ($thisYr > 2009){
print "<br/>copyright &copy; 2009-" . $thisYr;
}
else {print "<br/>copyright &copy; 2009";}

?>

You can simply copy andpaste this into your page where you need it, but I will explain it below:

1. the <?php opens the php code and the ?> closes it. Whatever you write in there is php script.
2. We get the year from the date() function, and by putting the capital 'Y' all we get is the year. So that assigns the current year to the variable called $thisYr. Note that capitalization is important and shouldn't be changed, otherwise you will have trouble with the script.
3. The 'if {} else {}' code blocks are easy to understand. If it is this year, print the one thing, if it isn't print the other. You can change the date to whatever your beginning year is for the copyright.
4. The '&copy;' bit is written like this, but the html page will render it like this '©' It is just the unicode symbols for the little copyright icon.

And that's basically it. You can extend it a little by putting hypertext to the copyright to a link that defines your copyright terms, such as, perhaps, a use and share and backlink or something like that.

If you are creative enough, you may think of other uses for such a simple script. It is one of the wonderful things about these types of scripting languages...small bits of code do really cool things.