Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Friday, 2 January 2009

UNIX_TIMESTAMP 'bug'

Not really a bug as such - its just an issue you have to consider and work around - in the UNIX_TIMESTAMP function in mySQL and is related to the twice-annual 'summer time'/'daylight savings time' clock change. My code was saving the right date into the database - it was just fetching it back again that introduced an error. I tried to store the date 31st March 2009 but when I recalled the data, it was displayed as 30th March. So I added some comments to my HTML to see what was happening:

<!-- my_date_field = 2009-03-31 -->
<!-- UNIX_TIMESTAMP(my_date_field) is 1238454000 = 30-03-2009 -->

If you use a 'date' field, the date ends up a day out, if you use a 'datetime' field, it ends up an hour out.

Fortunately there is a workaround:

UNIX_TIMESTAMP(your_date_field)

...has to be replaced with:

UNIX_TIMESTAMP(CONVERT_TZ(your_date_field, '+0:00', 'SYSTEM'))