Why does the minus operator give a different result than the TIMESTAMPDIFF () function in mysql?

Since TIMESTAMP in mysql is stored as a 32-bit value representing the time interval from 1970-jan-1 0:00:00 in seconds, I assumed that using the minus (-) operator in TIMESTAMP values ​​would give a difference of these values ​​in seconds. Not really:

+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:29:59") |
+---------------------------------------------------------------------+
| 41.000000                                                           |
+---------------------------------------------------------------------+
1 row in set (0.05 sec)

mysql> select timestampdiff(SECOND,TIMESTAMP("2010-04-02 10:30:00"),TIMESTAMP("2010-04-02 10:29:59"));
+-----------------------------------------------------------------------------------------+
| timestampdiff(SECOND,TIMESTAMP("2010-04-02 10:30:00"),TIMESTAMP("2010-04-02 10:29:59")) |
+-----------------------------------------------------------------------------------------+
| -1                                                                                      |
+-----------------------------------------------------------------------------------------+

mysql> select TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:30:01") ;
+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:30:01") |
+---------------------------------------------------------------------+
| -1.000000                                                           |
+---------------------------------------------------------------------+

+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:31:00") |
+---------------------------------------------------------------------+
| -100.000000                                                         |
+---------------------------------------------------------------------+

The one minute difference seems to be 100, not 60.

Why is this?

+3
source share
2 answers

Just a wild hunch, but maybe you are throwing strings into an integer in three cases?

20100402103000 - 20100402103100 = -100

20100402103000 - 20100402103001 = -1

20100402103000 - 20100402102959 = 41

In another case, the correct conversion.

+2

- UNIX_TIMESTAMP().

TIMESTAMP() () '2003-12-31 00:00:00'.

,

SELECT TIMESTAMPDIFF(SECOND,NOW(),TIMESTAMP("2010-04-02 19:29:59"));
0

Source: https://habr.com/ru/post/1739616/


All Articles