简体   繁体   中英

MySQL datetime vs TIMESTAMP with time zones

OK I have a MySQL DB in UTC timezone, I don't want to change that (it is a AWS RDS). I am running all queries through php_mysqli I am using the following after connection to set timezone:

SET time_zone = "-05:00"

I have a table test with 2 fields: date_add => DATETIME date_upd => TIMESTAMP

The MySQL NOW() and CURRENT_TIMESTAMP match and are in right timezone. Confirmed my running:

SELECT NOW()
SELECT CURRENT_TIMESTAMP

When I run the following:

INSERT INTO `test` SET `date_add`=NOW()

then

SELECT * FROM `test`

I get matching date_add and date_upd with correct time zone

However when I look at the raw table in CLI or phpMyAdmin the results are different, like in different timezones?

+---------------------+---------------------+
| date_add            | date_upd            |
+---------------------+---------------------+
| 2013-06-07 15:57:09 | 2013-06-07 20:57:09 |
+---------------------+---------------------+

Not sure if the 2 fields are just set to differnt time zones are what is going on any help with this would be greatly appreciated... thx

my guess would be that set timezone just sets the local environment variable and has no affect on what is actually saved. it just affects how it is viewed when queried.

in phpmyadmin you dont do the same set first, so you see it with default timezone.

That is because datetime does not have a timezone, whereas timestamp have timezone. http://dev.mysql.com/doc/refman/5.0/en/datetime.html

In MySQL, timestamp in fact is always in UTC, ie, when you insert a timestamp at your local time zone to db, it's automatically converted to UTC on the database server. So it's consistent everywhere. I'd recommend that you always use timestamp if there are clients in different time zones. Also timestamp is more efficient than datetime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM