Reputation: 5663
I'm using PHP/MySQL and I've always used DATETIME to store created
and updated
values, but I'm thinking there may be a better way.
Should I be using TIMESTAMP instead, and if so, why?
Upvotes: 3
Views: 1117
Reputation: 4315
Timestamps are stored in UTC time in the database. Then, depending on your timezone setting, any time you fetch it it will automatically adjust the offset appropriately. If you are concerned with setting timezones for your application then definitely go with Timestamps.
Upvotes: 0
Reputation: 120937
The documentation states:
TIMESTAMP values are converted from the current time zone to UTC for storage, and converted back from UTC to the current time zone for retrieval. (This occurs only for the TIMESTAMP data type, not for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis, as described in Section 9.6, “MySQL Server Time Zone Support”. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable.
So you may want to use TIMESTAMP
if you want your date/time stored in UTC instead of in the current time zone.
Upvotes: 1