在MySQL中计算两个日期时间之间的差异

时间:2022-08-19 21:29:38

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()).

我将最后一次登录时间存储在MySQL的datetime类型文件中。当用户登录时,我希望得到最后一次登录时间和当前时间(我现在使用的是NOW()))之间的差异。

How can I calculate it?

我怎么计算呢?

1 个解决方案

#1


217  

USE TIMESTAMPDIFF MySQL function. For example, you can use:

使用TIMESTAMPDIFF MySQL函数。例如,你可以使用:

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.

在您的示例中,TIMSTAMPDIFF函数的第三个参数是当前登录时间(NOW())。第二个参数是最后一次登录时间,它已经在数据库中。

#1


217  

USE TIMESTAMPDIFF MySQL function. For example, you can use:

使用TIMESTAMPDIFF MySQL函数。例如,你可以使用:

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.

在您的示例中,TIMSTAMPDIFF函数的第三个参数是当前登录时间(NOW())。第二个参数是最后一次登录时间,它已经在数据库中。