views:

330

answers:

2

I have built a small forum where users can post messages. My server is in the United States, but the userbase for the forum is in Taiwan (+15hrs).

When someone posts to the form, I store the time in my mySQL database in the format YYYY-MM-DD HH:MM:SS. When I look in the database, the time displays the proper time (the time that the person in Taiwan posted it).

However, when I use UNIX_TIMESTAMP to get the date out of the database, the time is altered.

Example:

  1. I post something to the forum. The datetime on my wrist watch is 2009-10-2 11:24am (Taiwan Time)
  2. I look in the database and it says the datetime is 2009-10-2 11:24am (same time as my wrist watch. good!)
  3. Then when I use UNIX_TIMESTAMP to display the date on my website, it shows as 2009-10-03 4:22 pm (bad! it applied an offset)

Is there a way I can get UNIX_TIMESTAMP to stop converting the time (applying an offset) when I query the date from the database?

Extra Info:
I'm using PHP
I have set the timezone in my PHP to Taiwan (date.timezone = Asia/Taipei)
If a user is in another timezone than Taiwan, I want it to convert the time to Taipei time. The site is nearly 100% Taiwan used so I just want Taiwan time to show all the time even if they're in another timezone.
I display the date in lots of areas around the site in different date() formats.
Basically everything works great except that when I use UNIX_TIMESTAMP to query the data out, it applies an offset to the time.

Thanks!

+1  A: 

MySQL takes system's default timezone setting unless told otherwise, it explains the problems you are having; take a look at MySQL's time zone reference manual for more details. Based on my past experience I've come to a conclusion UTC is the best choice for storing date and time; when displaying it to the user, they are converted to user's timezone.

If possible, change all date and time entries in the DB to UTC, configure timezone in PHP usingdate_default_timezone_set()and make sure to convert it properly when rendering it to the user and when storing it in the database as well. If storing UTC values is not an option, you may simply convert them by following time zone reference guide the same way as with UTC.

What you need to do is grab raw date and time from the database and then use PHP's DateTime to convert it. Take a look at DateTimeZone as well.

David Kuridža
Hi David. I am currently storing the times in the DB as UTC (That's why I need to use UNIX_TIMESTAMP to retrieve it from the db). Setting the timezone using the php.ini I believe is also the same as using date_default_timezone_set(). The problem is extracting the UTC from the db because I want the date in a unix format so that I can have an array of different ways to display the date. Unfortunately when I convert the time to unix using unix_timestamp (from with sql) it converts the time to GMT
justinl
The idea above is basically what you have written below, grabbing UTC date and time from the DB and then converting it to the time zone you need; or do that in the SQL directly, see MySQL's time zone reference manual for more details.
David Kuridža
A: 

I have found a possible solution which is to just retrieve the date from the database without converting it to Unix time, and then just using strtotime(); to convert it to Unix time. Basically instead of converting using sql, i'm converting using php. The only things I don't like about it are: strtotime() < I'm not sure how reliable this function is, and I have to go and change about 100 places where i'm using UNIX_TIMESTAMP (doh!)

Are there any other ways?

justinl