How to convert DateTime to a number in MySQL?

后端 未结 4 623
再見小時候
再見小時候 2020-12-31 05:40

How can I get the total number of seconds since \'1970-01-01 00:00:01\' from a DateTime instance in MySQL?

4条回答
  •  误落风尘
    2020-12-31 06:13

    UNIX_TIMESTAMP(datetime) force a localization of the datetime, which unlike the timestamp, is stored "as is".

    You need actually any of the following, for discarding the UTC correction:

    UNIX_TIMESTAMP(CONVERT_TZ(datetime, '+00:00', @@session.time_zone))

    or:

    TIMESTAMPDIFF(SECOND,'1970-01-01 00:00:00',datetime)

    Refs: 1, 2, 3, 4

提交回复
热议问题