Sign in to your account



This field is required


forgot your password?


New to the site? Create an account →

MySQL Date/Time fields vs Integers

tcr! · Aug 30, 2011 at 5:41 pm

The more I think about passwords and data types, the more I started thinking about changing the various data types for the various database fields. My mind thinks all the time, it’s homicidal.

I’m not a database programmer by any means, but I do regret storing date/times as TIMESTAMP. I wish I would’ve used a simple INT populated with Unix time. For one reason only – timezones.

As an example, 2011-08-30 19:20:20 tells me nothing about the timezone. Why is this important? Pretend there’s a worldwide, online discussion starting at 7:20 pm and you’re invited!

MySQL stores TIMESTAMP fields based on a timezone setting that you can set and update via MySQL and PHP, etc. Yuck. Why the yuck? Because if I dump the database from a server in PST and import it to a server in CST, the times will be wrong. Yuck.

Maybe there’s a slick workaround for this that I don’t know. Honestly, I just want the time stamp to be reliable. Any programmer looking at 1314705293 will probably guess what it is and know how to work with it.

And since I can’t leave well enough alone, SELECT UNIX_TIMESTAMP(created_on) AS created_on_unix ... UPDATE created_on = created_on_unix has become my new best friend.

#tcr #webdev #mysql

Brandon Godwin · Aug 30, 2011 at 10:51 pm

Since MySQL 5 I believe, the TIMESTAMP data type is converted to UTC when it's stored and converted back into the timezone of the server or connection set timezone when retrieved. Storing the date and time as a TIMESTAMP allows you to perform date operations in the query, such as adding or subtracting time from it for a WHERE clause.

However, make sure you use TIMESTAMP and not DATETIME for this. DATETIME does not handle the timezone conversion.

Reply

It’d be better if you signed in before commenting


Post

tcr! tcr! · Sep 1, 2011 at 6:14 am

Thanks for the tip! I didn't know that.

Reply

It’d be better if you signed in before commenting


Post

Add a comment

It’d be better if you signed in before commenting


Post



Latest articles for #mysql

Aug 20th, 2018 at 3:16:17 pm

Aug 20th, 2018 at 3:16:17 pm

Aug 20, 2018 at 3:16 pm

Writing a valid, functional, and strict MySQL regular expression for…

This Is About Answering Questions

This Is About Answering Questions

Feb 14, 2013 at 1:21 pm

mysql - how to mysqldump remote db from local machine -1, cause this…