Auto-updating Dates with Timezones and PHP This article was posted on Tuesday, 3rd January 2012

Happy New Year! The last few days I've seen a number of articles written, regarding the PHP date() function, which can be used to ensure the copyright date on the footer of your website (assuming you have a website written in PHP) is always the current year. I'm sure you'll agree that there is nothing worse than seeing 'Copyright © 2009' when it's the sparkly year of 2012.


If you are unaware of the date() function, it basically allows you to output the current date on your PHP website. For example, if you wanted to display todays date, you could use this:

<?php echo date('l, jS F Y'); ?>

This will output Tuesday, 3rd January 2012. Due to my CMS, I can't inject PHP into this content, so please take my word for it. The l, j, S, F, Y all refer to different date aspects- in this case, l is the day of the month, j is the date, S is the ordinal suffix, F is the full name of the month and Y is the four digit year. You're not limited to just these elements, you can mix it up. I've included some links at the bottom for more information.

Anyway, in relation to updating your copyright date, to produce the current year you could simply stick in <?php echo date('Y'); ?> in the appropriate place. Depending on how long the site is up, you could do something like:

<p>Contents &copy; 2009 - <?php echo date('Y'); ?></p>

This will output 'Contents © 2009 - 2012'. However, there is one issue that may cause you hassle, which I have not seen addressed in any of the articles I have read.

The Issue

Sometimes, the servers that host your website do not have a timezone set. If this is the case, PHP throws an error which can either stop the website from loading correctly, or just not display the date- depending on the server settings. I do not have this problem with my preferred hosting company, Blacknight, as they have their timezones set at the php.ini document on the server.

I do, however, have this problem on my local hosting environment- I haven't set the timezone on my local development machine. It's a subtle reminder to always declare the timezone on the websites I'm working on- I like my sites to be self contained, able to be moved from hosting company to hosting company, with minimum hassle, should the need arise.

Declaring timezones

Aside from avoiding potential errors popping up, declaring a timezone is essential if you are doing delicate date/time related work. For instance, I have on the Pixelcode contact page a script that displays the current local time and shows whether I am open or closed for business. This would be useless if the timezone was set to New York or California. As it happens, my host is in the same timezone as me, but why take the risk when the solution is so simple?

The solution

I use the following, before any date function is used:

ini_set('date.timezone', 'Europe/Dublin');

So, in full, to output the current year (or any day and date):

<?php ini_set('date.timezone', 'Europe/Dublin'); echo date('Y'); ?>

I have linked to the full timezone list below. I personally use the above method in the footers of websites I make, or else I include it in the bootstrap files of the site, declaring it once to save a bit of time.

Resources and Links

PHP Solutions - David Power. I would be lost and useless without this book, now in it's second edition. Highly recommended. Full date() function format characters Timezones

If you have any queries or comments, please use the contact form.

There are more articles in the Archive

Contents © 2008 - 2024 John Rainsford. All rights reserved. Hosted by Blacknight.