views:

417

answers:

2

If you need your web application to translate between timezones on a per-user basis, why not use TIMESTAMP across the board for all date/time fields? Consider that TIMESTAMP values are stored in UTC and converted to the timezone set for the connection when retrieved.

I have asked this question on IRC, read the MySQL documentation, searched Google extensively, and asked my co-workers, and I have yet to find a compelling reason to not use TIMESTAMP.

Note: I understand TIMESTAMP has a limited range of 1970 - 2038; that is not going to be an issue in my case. Also, I am using PHP with MySQL.

+1  A: 

I think your answer is here:

I understand TIMESTAMP has a limited range of 1970 - 2038; that is not going to be an issue in my case.

I would be careful making assumptions about the longevity of projects especially when it comes to database schemas. Databases have a tendency to remain in place and in use long after the applications that used them have gone away.

Andrew Hare
Surely MySQL would have released a patch for the 2038 issue--especially considering the vast majority of computers will be 64-bit by then?
Chad Johnson
Perhaps they will, but part of being a successful developer is to plan for the worst rather than hope for the best.
Andrew Hare
+1  A: 

DATETIME is for arbitrary dates and times that you utilize in your data.

TIMESTAMP is for when you want the time to be automatically updated. If you want to know when the row was inserted/updated, use a TIMESTAMP.

Also, keep in mind that a TIMESTAMP is only stored in UTC -- it is converted to the local timezone of the server before it is transmitted back as part of a query.

In your case, you're better off using a DATETIME. If you're worried about UTC, that's fine -- just create your dates in UTC rather than using the local time. Use UTC_TIMESTAMP.

Randolpho
Thank you for the response, but I am lost as to why I am better off using DATETIME, considering I would have to manually do the conversions 100s of places throughout my codebase, wherever I use PHP date functions, when the database manager could do this work for me.
Chad Johnson
You have to do that date conversion anyway. TIMESTAMP will return converted to the local server time. That's not the user's timezone. You should store your dates in UTC and convert them to the user's timezone at the UI level.
Randolpho
What if I run "SET time_zone='the users timezone'" whenever a database connection is made within the user's session? Would that be a good solution, in your mind?
Chad Johnson
Yes, that would work. But I still suggest that you handle it in your UI layer.
Randolpho
Is your recommendation based solely on the fact that TIMESTAMP is only intended for tracking record creation and update dates/times? Are there any other compelling reasons?
Chad Johnson
It's a logical/layering thing. You don't want to make what is essentially a UI/display decision at your data layer level. I prefer to have the actual data in my hands at the business layer level. If you specify timezone at the data level, you lose having that information at the business layer level. Odds are it won't affect things that much in your case, but you never know when some day down the line that decision might cause you pain. Better to pass the full UTC date to your UI layer and have any displays of that date convert to the user's timezone.
Randolpho