MySQL日期格式

Does anyone know what would be the right type of a MySQL column in order to store the date in the following format? Ex: 2012-11-11T11:09:28+00:00

Update: The part I was interested in was how to store the timezone +xx:xx of the user that took the action and the question was if there's is a MySQL date format that would include the timezone as well. Read the MySQL docs and didn't find any relevant information, that's why I wrote here.

There's no DATE/TIME column type in MySQL that includes the timezone. It's also not really necessary, since a timestamp is a timestamp, it's an absolute point in time. The timezone is only relevant if you want to format the timestamp as a local time, but MySQL stores dates as absolute points in time, regardless of timezone.

If you want to store a value including timezone information, you'll have to store it as a string.

But, the better strategy is this anyways:

  • normalize all times in your application to one timezone, UTC being a good choice
  • store that normalized time in the database
  • store a user preference for his/her preferred timezone
  • when fetching times from the database to display, convert them from UTC to the desired timezone of the user

That's the typical way to handle this. You can also store a timezone in another column next to the timestamp column, if that makes sense for your app:

`time_utc` (DATETIME) | `timezone` (VARCHAR)
----------------------+---------------------
2012-11-11 11:11:11   | Europe/Berlin

This way you have unified timestamps in your database to do calculations/queries on, while being able to format them in a local time when needed.