1

So, I have isOnline (boolean) and lastTimeSeen (DateTime) fields in my users table.

Everytime a user loads a page on my application, lastTimeSeen will be updated for him.

Then, I want to make a SQL query that will retrieve all users that have the isOnline column equals to 1 and where lastTimeSeen column is superior to 5 minutes to set isOnline to 0.

Though, I have no idea how to compare a DateTime with current time in order to check if the user has not been seen in the last 5 minutes.

Do I have to do that with PHP?

UPDATE FROM users WHERE isOnline = 1 AND lastTimeSeen => comparison to make

Could you guys help me with that?

Snyte
  • 93
  • 6

2 Answers2

3
UPDATE users 
SET isOnline = 0
WHERE isOnline = 1 
AND lastTimeSeen < now() - interval 5 minute
juergen d
  • 201,996
  • 37
  • 293
  • 362
1

You can use:

WHERE MY_DATE_FIELD <= NOW() - INTERVAL 5 MINUTE
Tarreq
  • 1,282
  • 9
  • 17