English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The difference between two timestamps in MySQL, both in seconds?

Now let's see the following method to calculate the time difference between two timestamps (in seconds).

Method

The following is a query that calculates the difference between two timestamps.

mysql> SELECT TIMESTAMPDIFF(SECOND, '2018-10-17 11:51:55', '2018-10-17 11:51:58');

The following is the output in seconds.

+---------------------------------------------------------------------+
| TIMESTAMPDIFF(SECOND, '2018-10-17 11:51:55', '2018-10-17 11:51:58') |
+---------------------------------------------------------------------+
|                                                                   3 |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)

Method2

The following is a query that calculates the difference between two timestamps using UNIX timestamp.

mysql> SELECT UNIX_TIMESTAMP('2018-10-17 11:57:50') - UNIX_TIMESTAMP('2018-10-17 11:57:45') as DifferenceInSeconds;

The following is the output in seconds.

+---------------------+
| DifferenceInSeconds |
+---------------------+
|                   5 |
+---------------------+
1 row in set (0.00 sec)