PHP / MySQL显示用户上次登录后的时间 [英] PHP/MySQL Display time since user's last login

查看:295
本文介绍了PHP / MySQL显示用户上次登录后的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我应该在数据库中存储什么样的价值。



我在想我应该这样做,但是我不知道如何:



存储自发行日期开始的年份,日期,小时,分钟,如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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆