Frederik
Frederik

Reputation: 14546

Converting a UTC time string to Unix time

In my database, I'm storing time as a BIGINT UTC value.

In my test data SQL dump I'd like to see the time in UTC for readability. However, the MySQL unix_timestamp function expects time in the local time zone, which I'm trying to avoid.

I've looked around and the only way I've found of converting the UTC time string to a unix time stamp is using this expression:

SELECT unix_timestamp(CONVERT_TZ('2011-08-08 06:00:00','UTC',@@global.time_zone))

Is there a better way to do this?

Upvotes: 0

Views: 843

Answers (1)

Ariel
Ariel

Reputation: 26753

If you stored it as a timestamp instead of a bigint it would happen automatically just by setting the timezone.

Also, I don't really understand what you are doing. You are storing unixtime as your bigint column correct? So I don't get why you are using the unix_timestamp() function - that converts TO a unixtime, not from it.

I guess this is what you need:

SET time_zone = '+0:00';
SELECT FROM_UNIXTIME(col) FROM table;

Upvotes: 1

Related Questions