Automate your website copyright date

Now that the new year is here, many of us will forget to change the © copyright date on our many websites. I’ve never really been sure why developers don’t always automate this process and therefore not have to worry about it at the change of the year. In this short post I will show you some quick methods of automating this process.

PHP

If your pages are written in PHP, all you need to do is to add this simple line in the place that you want the date to appear:

<?php print date('Y'); ?>

JavaScript

You can also use this simple JavaScript code to generate the year:

<script>document.write(new Date().getFullYear());</script>

.NET

The two methods for C# and VB are very similiar, but I’ve included both for completeness.

C#:

<%= DateTime.Now.Year.ToString() %>

VB:

<%= DateTime.Now.Year.ToString %>

Python

In Python, all you have to do is import the datetime library and add the line:

print datetime.date.today().year

Ruby

Thanks to Mark Embling, the Ruby code is:

<%= Time.now.year %>

Of course there are other languages and methods of doing this, these are just a few. I will add more as I find them or if others suggest that I should.

If you have any other suggestions of ways to automatically output the year on a webpage, or have other languages to suggest, please do so.