Code of the Year

Like most people, I have difficulty writing my first cheque of the new year. I always write the wrong year. Apparently, I am also like most web sites.

Many big, respectable and seemingly well-maintained sites show a previous year in their copyright footer. Some are painfully out of date.

If this was something that must be done manually, I could understand. But the content on a web page is not written with a Bic Grip Roller. It’s generated by a computer that knows the current year.

Here’s some code you can use to keep your copyright current. Examples for Perl, Python, etc. are appreciated!

In Javascript:

<script language="javascript" type="text/javascript">
//<![CDATA[
var mydate = new Date();
var year= mydate.getYear();
if (year<2000){year+=1900};
if (year>2100){year-=1800};
var time = ( year );
document.write(time);
//]]>
</script>

In PHP:

<?php echo date("Y");?>

In parsed HTML (aka SHTML, must be enabled in Apache):

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

In Java (JSP, thanks to Tim!):

<%= (new SimpleDateFormat("yyyy")).format( new Date() ) %>

In Smalltalk (thanks to Tim!):

^Date today year asString

In Ruby (thanks to Scott Boms):

add the following to a helper (eg. application_helper)

def copyright_year
   t = Time.now
   t.year
 end

then in the view file, just put <%= copyright_year %>



About this entry