Time zone offset in PHP

software development

#1

I capture the date in various PHP/MySQL interactions on my site. I recently moved my domain to dreamhost, and quickly realized that I took for granted that the my old host’s servers were in the same time zone as my site. Now all the dates that get entered are 3 hours behind. MySQL strores the dates in a datetime field in the format YYYY-MM-DD HH:MM:SS (24-hour time), and is generated using PHP date(‘Y-m-d H:i:s’).

Is there an easy way to add 3 hours to the time I’m getting off the server? This of course means that the day/month/year may also be affected in certain circumstances. Is there a way to set the server time to EST rather than PST?

Thanks for your help!

Edit: So I am impulsive and post when I should just work this stuff out, and this is a really easy problem to solve:
$servertime = date(‘U’); //gets PST Unix timestamp
$est_timestamp = $servertime 10800 //add 3 hours
$est_date = date(‘Y-m-d H:i:s’, $est_timestamp); //format for MySQL DATETIME field

Voila.

Still wondering if you can alter the time delivered from the server directly. This might be ridiculous, but I know nothing of how these things work.


#2

What I do is set the timezone in the .htaccess file, to avoid having to do adjustments in scripts.

First, use the the “tzselect” command to find out your timezone. (You could also use “EST5EDT”, “UTC”, etc.)

To change the timezone for Apache (and PHP), add this to the .htaccess file in your web site’s root directory:

SetEnv TZ America/Montreal

Unfortunately, this does not affect MySQL… yet. MySQL 4.1 will let you change the time zone using “SET time_zone = ‘America/Montreal’”

To change the timezone used for interactive shells, put this in .bashrc: (varies if you’re using a different shell)

export TZ=America/Montreal