'Calculate difference between two datetimes in MySQL
I am storing the last login time in MySQL in, datetime-type filed. When users logs in, I want to get the difference between the last login time and the current time (which I get using NOW()).
How can I calculate it?
Solution 1:[1]
USE TIMESTAMPDIFF MySQL function. For example, you can use:
SELECT TIMESTAMPDIFF(SECOND, '2012-06-06 13:13:55', '2012-06-06 15:20:18')
In your case, the third parameter of TIMSTAMPDIFF function would be the current login time (NOW()). Second parameter would be the last login time, which is already in the database.
Solution 2:[2]
my two cents about logic:
syntax is "old date" - :"new date", so:
SELECT TIMESTAMPDIFF(SECOND, '2018-11-15 15:00:00', '2018-11-15 15:00:30')
gives 30,
SELECT TIMESTAMPDIFF(SECOND, '2018-11-15 15:00:55', '2018-11-15 15:00:15')
gives: -40
Solution 3:[3]
If your start and end datetimes are on different days use TIMEDIFF.
SELECT TIMEDIFF(datetime1,datetime2)
if datetime1 > datetime2 then
SELECT TIMEDIFF("2019-02-20 23:46:00","2019-02-19 23:45:00")
gives: 24:01:00
and datetime1 < datetime2
SELECT TIMEDIFF("2019-02-19 23:45:00","2019-02-20 23:46:00")
gives: -24:01:00
Solution 4:[4]
I don't think the accepted answer is appropriate. For example, if the difference between last login time and current time is 8 hours then getting the difference in seconds is illogical. The correct format will be in hours, minutes and seconds. I have illustrated this as follows -
Here, I create a table login_info table to store login information of users.
CREATE TABLE login_info (
-> user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> last_login DATETIME NOT NULL,
-> PRIMARY KEY (user_id)
-> );
Then I populate the table using some random values -
INSERT INTO login_info (last_login) VALUES
-> ("2021-09-22 09:32:44"),
-> ("2021-09-22 13:02:57"),
-> ("2021-09-21 23:43:21"),
-> ("2021-09-22 04:43:39"),
-> ("2021-09-22 17:23:21");
Now I calculate the difference between last_login and current_time as follows:
CREATE TABLE login_dur_in_sec AS
-> SELECT user_id,
-> TIMESTAMPDIFF(SECOND, last_login, NOW()) AS diff
-> FROM login_info;
SELECT * FROM login_dur_in_sec;
+---------+-------+
| user_id | diff |
+---------+-------+
| 1 | 28580 |
| 2 | 15967 |
| 3 | 63943 |
| 4 | 45925 |
| 5 | 343 |
+---------+-------+
CREATE TABLE hour_section AS
-> SELECT user_id,
-> FLOOR (diff / 3600) AS hour_part
-> FROM login_dur_in_sec;
CREATE TABLE minute_section AS
-> SELECT user_id,
-> FLOOR (MOD (diff, 3600)/ 60) AS minute_part
-> FROM login_dur_in_sec;
CREATE TABLE second_section AS
-> SELECT user_id,
-> MOD (MOD (diff, 3600), 60) AS second_part
-> FROM login_dur_in_sec;
CREATE TABLE login_dur AS
-> SELECT h.user_id, h.hour_part, m.minute_part, s.second_part
-> FROM hour_section AS h INNER JOIN minute_section AS m
-> ON h.user_id = m.user_id
-> INNER JOIN second_section AS s
-> ON m.user_id = s.user_id;
CREATE TABLE login_dur_trunc AS
-> SELECT user_id,
-> CONCAT (hour_part, ":", minute_part, ":", second_part) AS login_duration
-> FROM login_dur;
SELECT * FROM login_dur_trunc;
+---------+----------------+
| user_id | login_duration |
+---------+----------------+
| 1 | 8:14:46 |
| 2 | 4:44:33 |
| 3 | 18:4:9 |
| 4 | 13:3:51 |
| 5 | 0:24:9 |
+---------+----------------+
Here, the answer given by @Adi won't work always as pointed out by @CaiusJard.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | trejder |
| Solution 2 | ingconti |
| Solution 3 | |
| Solution 4 | Payel Senapati |
