Search code examples
mysqlutcgmt

Convert epoch time to gmt using mysql


I am running into trouble converting a time to utc/gmt.

 select from_unixtime(1623167869);
 2021-06-08 11:57:49

GMT should return 2021-06-08 14:57:49.


Solution

  • FROM_UNIXTIME

    The return value is expressed in the session time zone.

    https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-unixtime

    > SET time_zone = '+00:00';SELECT @@SESSION.time_zone;select from_unixtime(1623167869);
    Query OK, 0 rows affected (0.000 sec)
    
    +---------------------+
    | @@SESSION.time_zone |
    +---------------------+
    | +00:00              |
    +---------------------+
    1 row in set (0.000 sec)
    
    +---------------------------+
    | from_unixtime(1623167869) |
    +---------------------------+
    | 2021-06-08 15:57:49       |
    +---------------------------+
    1 row in set (0.000 sec)
    
    > SET time_zone = '-02:00';SELECT @@SESSION.time_zone;select from_unixtime(1623167869);
    Query OK, 0 rows affected (0.000 sec)
    
    +---------------------+
    | @@SESSION.time_zone |
    +---------------------+
    | -02:00              |
    +---------------------+
    1 row in set (0.000 sec)
    
    +---------------------------+
    | from_unixtime(1623167869) |
    +---------------------------+
    | 2021-06-08 13:57:49       |
    +---------------------------+
    1 row in set (0.000 sec)