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:

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

  • On January 3, 2009 at 12:47 pm,Ajith Edassery | DollarShower wrote:

    John Chow has copied your idea :lol: Check his blog

  • On January 3, 2009 at 12:55 pm,Velvet Blues wrote:

    Whoa! I don’t know if I like that!!! haha.

    The code snippet is hardly unique; this is probably the only way to do it in PHP. I just beat him to the punch, and published my article first.

    But who knows, maybe he’s reading my blog. lol.

    Thanks for the tip. I WILL FOLLOW UP. :-) j/k

  • On January 3, 2009 at 1:40 pm,Ajith Edassery | DollarShower wrote:

    I have had this experience in the past. Something that I wrote first has appeared with very minor changes on big blogs (sometimes even as guest posts :lol: )

  • On January 3, 2009 at 1:59 pm,Velvet Blues wrote:

    Well, this is the web, and ideas are shared. But when it is a blatant ripoff, I do get a bit irritated. :-(

  • On May 3, 2009 at 7:18 pm,Brian wrote:

    You have a curly single quote right before the “Y” on the third line. Simply changed to a single quote and all worked well. Thank you very much.

  • On May 4, 2009 at 9:17 am,Velvet Blues wrote:

    @Brian: Thanks for the feedback. Yes, sometimes the output is modified by the software. Those two quotes surrounding the ‘Y’ should indeed be the same.

  • On November 23, 2010 at 1:10 am,Ivan wrote:

    Thanks! I’ve been looking for that code exactly. It works wonderfully.

  • On February 2, 2011 at 8:04 am,Richard Cummings wrote:

    Most people just use the year as in 2011. I like this then and now concept. Thx, Richard

  • On February 24, 2011 at 4:48 am,Paul Mason wrote:

    Hi,

    I’ve written a very similar tutorial with slightly different syntax:

    http://paulmason.name/blog/item/dynamic-php-copyright-date-code

Trackbacks

Trackback URL:

Leave a Reply

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