Best way to get unix timestamp for current UTC time in MySQL

littlejedi picture littlejedi · Jul 21, 2013 · Viewed 53.2k times · Source

I've been trying to use the following in mySQL to create a unix stamp for current UTC time

UNIX_TIMESTAMP(UTC_TIMESTAMP())

When I execute the query and get the result, it seems like mySQL is doing UTC conversation twice.

Eg. local time 9:07PM

Query above returned: 1374390482, which is next day 07:08:02 GMT, which is correct,

However, UNIX_TIMESTAMP(LOCALTIMESTAMP()) returns:

1374372551 02:09:11 GMT

which is the correct UTC unix timestamp.

So UNIX_TIMESTAMP automatically translates the date object's timezone to UTC (regardless if it's already UTC or not?)

Is there a better way or a single command just to get a UTC unix timestamp in mySQL?

Answer