views:

672

answers:

4

My server is in Dallas. I'm in New York City.. and both PHP and MySQL have configuration variables for setting the timezone.

How do I get them all to work together? What dates should I store in MySQL? How do I get PHP to handle changing the date based on the user's preference?

Bear in mind: I don't think I'm ever having PHP explicitly set the date, it's always using "NOW()" in queries.. however I foresee the need to do this. How would this be done?

I'm hoping SO's experience can help me out here.

A: 

Record your dates in GMT (zero offset) and then calculate the offset based on the local timezone (EST is +6, for example, so you'd add 6 hours to the GMT).

Check the Date docs for the date_default_timezone_set() function.

Just remember, when writing to the database, you'll have to change time zones, store the date, then change back. Likewise, when you're retrieving the date, don't forget to add the timezone offset.

Soviut
Your answer is not clear enough. There are several things up for configuration: PHP's timezone, MySQL's timezone, and the servers timezone. Are you saying I should set MySQL's timezone to be GMT?
I'm sorry but I still don't understand what you're getting at here. Could you provide an example, where the user specifies their timezone and the server is located in, oh let's say, Dallas.
The you need to use the Dallas timezone (assuming the server is set to that timezone) to convert to 0 GMT offset. Then if a user specifies a NY as a timezone it's easy to convert the 0 GMT offset to EST.
null
In other words 2:30am PST(GMT-8) is 10:30am GMT save the 10:30am time in MYSQL, when pulling from the database for EST(GMT-5) you'll display 5:30AM ... the -8 and -5 are hours.
null
+12  A: 

Use Unix Time everywhere. It's using UTC so it's the same for every timezone. Methods for dates usually convert to it and back from it using timezone information they have, so you would have yourself a correct time.

Alternatively you could use Unix Time only to transfer time from one computer to another (like from DB to your server running PHP, or to JavaScript client). There's functions to convert to it and from it in every language. For MySQL it is:

UNIX_TIMESTAMP(date)
FROM_UNIXTIME(unix_timestamp)

That way you could have your time properly formatted on the DB and in logs but still have correct local time everywhere.

vava
Couldn't agree more.
James Burgess
How do I implement this? NOW() in MySQL takes into account the timezone
UNIX_TIMESTAMP() ... nevermind. The only problem is I like using DATETIME because it's nicer to look at, doesnt have the 2038 bug.
You could use timestamp to transfer time between computers only. I've edited the answer to reflect this.
vava
Just a note:On January 19, 2038 the Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a "bit" more time.
Ivan
A: 

The mysql-server stores dates in a timezone independent format (UTC).
But before it stores the date it will be converted using its timezone.

U can change the mysql timezone per connection *1:

mysql_query('SET time_zone = "'.$timezone.'"');

You can also change the timezone per script.

date_default_timezone_set($timezone);

If you set them to the same timezone "2009-01-10 13:30:00" will mean the same thing to both mysql and php.

But keep in mind that the 2 servers have different internal clock values, so if you want to generate timestamps based on current time. Do that in mysql OR php.

*1) MySQL timezone support may require additional configuration. check the manual

Bob Fanger
+2  A: 

I prefer using dates and times in the native form with respect to the environment, that is, Unix timestamps in PHP and DATE/TIME/DATETIME/TIMESTAMP fields in MySQL. I translate both values into another using FROM_UNIXTIME() and UNIX_TIMESTAMP(). I prefer this instead of Unix timestamps, because native dates/times are much easier to read.