Now() not working as suspected

I use

INSERT INTO user_history (user_id, logged_in) VALUES (1,now());

to record the time I log in. it woks


But when I logout I sun

UPDATE user_history SET logged_out = now() WHERE session_id = 6

But when ck PHPMyAdmin, I see


why are both values being changed?

https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html

You should to replace TIMESTAMP type in your login/logout fields with datetime.

1 Like

that was it, now im trying to display the difference between the two times

SELECT logged_in, logged_out, TIMESTAMPDIFF(MINUTE,logged_in,logged_out) AS length FROM user_history WHERE user_id = 1

and


so the result is 1. how can I break i up to hours, minutes, and seconds?

https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timediff

instead of asking for the difference in minutes, ask for it in seconds, and do the math ™

:slight_smile:

1 Like

I used the gmdate()