PHP / MySQL自用户上次登录以来的显示时间

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

Why not just use a TIMESTAMP or a DATETIME? That's literally all you need.

You can store the timestamp of the last login and then just do the arithmetic. Maybe you should look at the DateTime::diff method to let PHP do the hard maths ;-)

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;