PHP / MySQL显示用户上次登录后的时间 [英] PHP/MySQL Display time since user's last login
问题描述
我在想我应该这样做,但是我不知道如何:
存储自发行日期开始的年份,日期,小时,分钟,如1/1/2000
然后在另一个时间,我将比较该值与2000年1月1日以来的当前年份,日,小时和分钟。显然,两者之间的差异将是用户上次登录以来的时间。
感谢任何人都可以提供的帮助。
提前感谢
mysql
将这样做您。它将存储自1970年1月1日以来的秒数。并计算差额。
尝试这样: SELECT NOW();
。你会看到现在的日期/时间。尝试 SELECT UNIX_TIMESTAMP(NOW());
,您将看到自Epoch以来的秒数(但您通常不需要)。
您应该每次用户登录时存储此值,这意味着将其插入表或更新last_login字段。
您有两个选项:
INSERT INTO user_login_history(user_id,login_date)VALUES(1,NOW());
和
更新用户SET last_login = NOW()WHERE user_id = 1;
请注意, last_login
和 login_date
字段必须是 DATETIME
类型(存储日期和时间)或 TIMESTAMP
或 DATE
(仅限日期)或 TIME
(仅限时间)。
另外,你可以在mysql中进行时间计算,使用 DATEDIFF
,如下所示:
SELECT NOW(),DATEDIFF(NOW(),DATE('2012-6-5'));
, SELECT DATEDIFF(NOW(),last_login)ASdays_since_last_loginFROM users;
I don't know what kind of value I should store in the database.
I'm thinking I should do something like this, but I don't know how:
Store the years, days, hours, minutes since a give date like 1/1/2000 Then at another time I will compare that value against the current years, days, hours, and minutes since 1/1/2000. Obviously, the difference between the two would be the time since the user's last login.
I appreciate any help anyone can provide on this. Thanks in advance
mysql
will do this for you. It will store the number of seconds since 1 Jan 1970. And calculate the difference.
Try this: SELECT NOW();
. You will see now's date/time. Try SELECT UNIX_TIMESTAMP(NOW());
and you'll see the number of seconds since Epoch (but you don't need that usually).
You should store this value everytime the user logs in, which means insert now() into a table or update a last_login field.
You have two options:
INSERT INTO user_login_history (user_id, login_date) VALUES (1, NOW());
and
UPDATE users SET last_login=NOW() WHERE user_id=1;
Note that the last_login
and login_date
fields will have to be of either DATETIME
type (to store date and time) or TIMESTAMP
or DATE
(only date) or TIME
(only time).
Also, you can do time calculations in mysql with DATEDIFF
, like so:
SELECT NOW(), DATEDIFF(NOW(), DATE('2012-6-5'));
In your table, SELECT DATEDIFF(NOW(), last_login) AS "days_since_last_login" FROM users;
这篇关于PHP / MySQL显示用户上次登录后的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!