Home >Database >Mysql Tutorial >How to Calculate the Time Difference Between Two Datetimes in MySQL?
How to Calculate the Temporal Difference Between Two Datetimes in MySQL
When maintaining user login information in MySQL's datetime data type, determining the time elapsed since a user's last login is essential. To perform this calculation, MySQL provides the TIMESTAMPDIFF function.
Query Syntax:
SELECT TIMESTAMPDIFF(<interval>, <start_datetime>, <end_datetime>)
Parameters:
Example:
Consider a scenario where you want to calculate the difference between the last login time (last_login_time) and the current time (NOW()) in seconds. You can use the following query:
SELECT TIMESTAMPDIFF(SECOND, last_login_time, NOW())
Sample Result:
The query will return the difference between the last login time and the current time in seconds. For instance, if the last login time is '2023-07-25 14:50:00' and the current time is '2023-07-25 15:05:30', the result will be 870 seconds.
The above is the detailed content of How to Calculate the Time Difference Between Two Datetimes in MySQL?. For more information, please follow other related articles on the PHP Chinese website!