Web Design and Development
The 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.)
© 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.
© 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: PHPTrackback URL:
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
John Chow has copied your idea
Check his blog
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
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
)
Well, this is the web, and ideas are shared. But when it is a blatant ripoff, I do get a bit irritated.
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.
@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.
Thanks! I’ve been looking for that code exactly. It works wonderfully.
Most people just use the year as in 2011. I like this then and now concept. Thx, Richard
Hi,
I’ve written a very similar tutorial with slightly different syntax:
http://paulmason.name/blog/item/dynamic-php-copyright-date-code