Myers Network
Myers Network

Reputation: 31

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

Upvotes: 1

Views: 4252

Answers (3)

Leon
Leon

Reputation: 3244

In MySQL, timestamp in fact is always in UTC, i.e., 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.

Upvotes: 1

user2591117
user2591117

Reputation: 21

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

Upvotes: 2

nl-x
nl-x

Reputation: 11832

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.

Upvotes: 0

Related Questions