PHP: Print the current year for copyright notices

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: PHP programming  

Hardcoding the year in a copyright notice is an easy way to get a blog or other website started, just placing the starting and ending years next to the copyright symbol. We have the very best of intentions to change this number right after the big ball drops in Times Square each year. We’re also human and, all too often, I see websites with copyright notices that are years behind. If your website is PHP-powered, this quick bit of code will put the current year in your copyright notice and off of your mind.


To change your copyright notice, or anything else you’d like that should display the current year, you must first find the script in which the copyright display is generated. If you are using a WordPress blog, for example, this is likely in your footer.php file in the directory of the theme you are using. If you aren’t sure which file holds this, you could try searching for the text © in all of the scripts that make up your website. This is the code that generates the copyright logo and should lead you directly to the file(s) involved.

Once you’ve found this, there are two possibilities for including the year information depending on how the script is setup. It is possible that the script is sending the line including the copyright information from an ‘echo’ command in PHP, something like:

echo '© 1812–2006 Olde Time Blogs';

In this case you need to modify this PHP echo command so that it calls the PHP date command. Note that the command could also work with double quotes instead of single quotes, just be sure to keep using the same kind.

echo '© 1812–'.echo date('Y').' Olde Time Blogs';

In PHP, the period symbol is used to stitch together strings, so the command above makes one string out of three.

The other possibility is that the PHP script is formatted as HTML content with separate PHP instructions enclosed by opening/closing PHP tags. This is more proper and common in applications like WordPress. In this case, inserting a dynamic, ever-correct year is a matter of changing:

© 1812–2006 Olde Time Blogs

to

© 1812– Olde Time Blogs

 

8 Comments -


  1. Sam IT said on December 17, 2008

    Why so many echos?

    Use one echo

    echo ‘© ‘ . date(‘Y’);

    or

    sprintf( ‘Copyright © %s – All right reserved.’, date(‘Y’) );

  2. veonexus said on January 23, 2009

    if in html, so:

    ©

  3. Emil said on March 12, 2009

    How about only this:

    ©

  4. Flight said on November 12, 2009

    thanks for your tip

  5. webb said on January 10, 2010

    wonderful tutorial

  6. Chris Bigler said on June 29, 2010

    You could also use an if else to post the originating copyright year -through- to the current year, like this:
    Copyright © . All rights reserved.

  7. Chris Bigler said on June 29, 2010

    would be your ‘custom variable’ from an included config file where site = your site name / company name

  8. Shan said on January 12, 2011

    Cool :)

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -