Categories
PHP

Setting a time zone offset in PHP

If your server is in a different time zone than your operations or your user-base, you're going to face some confusion when you use time formats to display things like time last updated, or post times.

The way to resolve this is to use the putenv() function.

[sourcecode language='php']
putenv("TZ=US/Eastern");
[/sourcecode]

To see this in action, run the following bit of code on one of your pages:

[sourcecode language='php']
echo "Original Time: ". date("h:i:s")."\n";
putenv("TZ=US/Eastern");
echo "Adjusted Time: ". date("h:i:s")."\n";
[/sourcecode]

I found a good listing of the time zones at http://www.theprojects.org/dev/zone.txt.

This is a good thing to put in the include that provides the opening page structure for your site.