Categories
Code/Tech Stuff

Code that Caters to Your Laziness

{{en|== Summary ==}} The font used in this ima...
Image via Wikipedia

Like clockwork, people build lists of resolutions and things to do for the new year. Many of the folks with websites end up scrambling to change the copyright dates sometime in the afternoon before they go out New Years Eve, or they end up doing it while hungover the next day. What is worse is when those of us that use a template created by someone else don’t realize that the coder got lazy and just threw the year in, and we don’t end up fixing the mistake until a few days later.

What I don’t get is why the folks that make these templates don’t have the self-updating code in there already. If you took all that time to create a template for people to use, you would think you would make it easier on yourself and put the code in there. I can understand it if you charged for support and just wanted to drum up some business, but it just seems silly.

To solve this problem, I did a bit of searching for code snippets to have your copyright date update when the calendar does.  Below are ways to do it in JavaScript, PHP, ASP, ColdFusion, and SSI.

JavaScript:

© <script language=
"JavaScript">
<!--
    today=new Date();
    year0=today.getFullYear();
    document.write(year0);
//-->
</script>

which gives you:

© 2010

or if you want a range of  dates and a little text

<script type="text/javascript">
<!-- copyright=new Date();update=copyright.getYear();document.write("Copyright © 2001-"+ update
+ "    (your company name)    All rights Reserved."); //-->
</script>

with that one it will give you

© 2001 – 2010 (company name) All Rights Reserved.

There is also a JavaScript generator available at Webweaver if you are feeling really lazy.

PHP:

<?php echo Date(Y);?>

or for a range of dates

 <?php
$c="2009";
$d=date("Y");
   if ($d==$c)
      echo "Copyright &copy; $d";
   else
      echo "Copyright &copy; $c-$d"; ?>

this one will produce a single date for the first year, and then the date range afterwards.

ASP:

<%=year(date)%>

or for the range of dates

 < %response.write("Copyright 2001- "&Year(Date))%>

or

< % Dim yy yy = DatePart("yyyy",Now()) %>Copyright © 2001-< %=yy%>

or

Copyright © 2001-< %=Year(Date)%>

ColdFusion:

<cfoutput>&copy; #Year(Now())#</cfoutput>

SSI:

<!--#config timefmt="%Y" --><!--#echo var="DATE_LOCAL" -->

or make your copyright statement a file fragment that can be pulled in with a simple:

<!--#include virtual="/path/to/copyright" -->

Since not every site is the same, it helps to have this list on hand. You just need to remember to check for them in any new templates that you decide to use, and/or remember to put them in a site you make on your own. Also, do not forget to update any forms you have (especially the ones involving money).