Blog Entries

Dynamic Copyright Code

April 22nd, 2010

Nowadays most, if not all, websites contain a small copyright blurb in their site footer. Take a look at the bottom of this site and you’ll see the text: “Copyright © 2009-2010 CopperBot Media” as an example. Even if you’re fairly new to web design you’re probably already savvy enough to know the ASCII character code for the copyright symbol and you might be thinking, “Creating a copyright blurb is pretty easy. Where are you going with this?” The problem with static copyright blurbs created in HTML is that they become obsolete at the end of the year. The purpose of this post is to provide you with a simple dynamic alternative (using php) that will update automatically and keep your copyright blurbs up to date.

Code Snippet

To begin simply copy and paste the code below into the footer of your website.

Note: You’ll need to remove the space in the line “< ?php". My code format plugin is misbehaving.

<span>
&copy;
< ?php
    ini_set('date.timezone', 'America/Chicago');
    $startYear = 2010;
    $thisYear = date('Y');
    if ($startYear == $thisYear) {
        echo $startYear;
    }
    else {
        echo "{$startYear}-{$thisYear}";
    }
?>
Website Design, Development and Search Engine Optimization (SEO) by
<a href="http://www.yourdomain.com" class="_blank">Your Domain Name</a>
</span>

Customize the Code for Your Site

To customize the code for your site there are a couple items you’ll need to change:

  1. First off you’ll need to change the value for the $startYear to the current year. For example: If you were building out a site in 2011 the line would read $startYear = 2011; (Be sure you include the ; mark at the end of the line or the script will break)
  2. The next thing to change is the timezone. Using this link: (Valid American Timezones) find the appropriate timezone for your client. In the code example I provided above my client was located in Dallas, Texas so I used the timezone ‘America/Chicago’. (Chicago and Dallas are both on Central Time)

The most important thing to change is the $startYear. Changing the timezone isn’t as important since all of our clients are located in North America anyhow. Worst case scenario the script will update the copyright date for the site a couple hours early or late (depending on where the client is located) come New Years.

Your .htaccess File

(Only necessary for static sites)

When a web page is accessed, the server checks the extension to know how to handle the page. Generally speaking if it sees a .htm or .html file, it sends it right to the browser because it doesn’t have anything to process on the server. If it sees a .php extension (or .shtml, or .asp, etc), it knows that it needs to execute the appropriate code before passing it along to the browser.

Because page for static sites end in .htm or .html we need to take one final step to be sure the php code is processed and the copyright date displayed as intended. In dreamweaver, or the text editor of your choice, create a new blank file. Paste the following line of code into the file you just created:

AddType application/x-httpd-php .html

Save this file with the name .htaccess

If for whatever reason a .htaccess file already exists then simply add this line of code at the tail end of the existing .htaccess file.

Note: This will create a hidden text file. You may need to adjust your settings for your operating system, text editor and FTP program in order to see the file once it is created. I’ve included some brief guides for Windows XP, Dreamweaver, & Filezilla below:

Hidden Files in Dreamweaver

  1. On the “Files” tab (the tab where the files and folders are displayed for your site) there is a little button that looks like a bullet list.
  2. Click this button and select the menu option View -> Show Hidden Files

Hidden Files in Windows Explorer (XP)

  1. Select the menu option Tools -> Folder Options
  2. When the Folder Options Window is displayed select the “View” tab.
  3. Scroll through the Advanced Settings until you find the option to “Show Hidden Files and Folders” Check this and hit Apply.

Hidden Files in Filezilla

  1. Select the menu option Server -> Force Showing Hidden Files

Note: Be sure to upload the .htaccess file with the rest of your site and you should be good to go. If you have any problems implementing this please let me know by leaving a comment and I’ll be happy to help.

2 Responses to “Dynamic Copyright Code”

  1. Kristen says:

    This is just what I was looking for! There are few tutorials that include a conditional statement if there isn’t yet a past year. Well done, mate!

  2. Jay says:

    Thanks Kristen! I’m glad I could help!

Leave a Reply




Spam protection by WP Captcha-Free