Left on the Web

MySQL Weirdness

I'm currently working with extending an existing application, so I'm working with a database someone else designed. Not a problem usually, except for the fact that this person chose to use the TIMESTAMP fieldtype, which I usually don't use. Now, when I think of a timestamp, I automatically think of a Unix Timestamp, which is the number of seconds since 01/01/1970. This is what most apps use. Not MySQL, as I found out. While trying to format the timestamp with the php date function, I got some weird data, so I checked the MySQL documentation. Turns out that for some reason beyond my understanding, MySQL does not use Unix Timestamps, but instead uses a string similar to the Datetime field, but without the special characters. So, for instance, august 1st 2005, 12:00 would be: 20050801120000. I just can't find any reason for MySQL doing this, but they did it. Of course, after I found this out, I am now formatting the timestamp field differently ;)

I'm currently working with extending an existing application, so I'm working with a database someone else designed. Not a problem usually, except for the fact that this person chose to use the TIMESTAMP fieldtype, which I usually don't use. Now, when I think of a timestamp, I automatically think of a Unix Timestamp, which is the number of seconds since 01/01/1970. This is what most apps use. Not MySQL, as I found out. While trying to format the timestamp with the php date function, I got some weird data, so I checked the MySQL documentation. Turns out that for some reason beyond my understanding, MySQL does not use Unix Timestamps, but instead uses a string similar to the Datetime field, but without the special characters. So, for instance, august 1st 2005, 12:00 would be: 20050801120000. I just can't find any reason for MySQL doing this, but they did it. Of course, after I found this out, I am now formatting the timestamp field differently ;)
Add comment

Comments

gravatar Horst: IIRC in some versions of MySQL TIMESTAMP also has a quite annoying side effect: When the row gets updated, so gets the TIMESTAMP, so that it’s always reflecting the time of the last update on the row (the first timestamp in this row is updated).

They’ve changed this behaviour as of 4.1.2.

http://dev.mysql.com/doc/mysql/en/timestamp-pre-4-1.html



© 2004 - 2009 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Ma.gnolia, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.2.3