MySql difference between two timestamps in Seconds?

后端 未结 3 1799
夕颜
夕颜 2021-01-30 13:22

Is it possible to calculate difference between two timestamps in Mysql and get output result in seconds? like 2010-11-29 13:16:55 - 2010-11-29 13:13:55 should give 180 seconds.<

3条回答
  •  盖世英雄少女心
    2021-01-30 13:40

    I do not think the accepted answer is a good universal solution!

    This is because the UNIX_TIMESTAMP() function fails for DATEs before 1970-01-01 (and for dates in the far future using 32 bit integers). This may happen easily for the day of birth of many living people.

    A better solution is:

    SELECT TIMESTAMPDIFF(SECOND, '2010-11-29 13:13:55', '2010-11-29 13:16:55')
    

    Which can be modified to return DAY YEAR MONTH HOUR and MINUTE too!

提交回复
热议问题