Home  >  Article  >  Database  >  Difference between two timestamps in seconds in MySQL?

Difference between two timestamps in seconds in MySQL?

WBOY
WBOYforward
2023-08-29 10:25:081513browse

Difference between two timestamps in seconds in MySQL?

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

method

The following is a query to calculate 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)

Method 2

The following is a query to calculate the difference between two timestamps with the help of 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)

The above is the detailed content of Difference between two timestamps in seconds in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete