Show users online

2

asked by anonymous 26.03.2014 / 03:43

2 answers

6

Just use the time functions of mysql itself. You do not even have to calculate the time in PHP:

$result = mysql_query("UPDATE novo_usuario SET validade=ADDTIME(NOW(),'0:05:00') WHERE ID=$id_user");
$result = mysql_query("SELECT * FROM novo_usuario WHERE validade>NOW()");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
...

Remember to use a column of type datetime (or timestamp ) to store the time. I suggest not using timestamp as the field name, not to confuse with the reserved word timestamp .

Solution explanation:

  • In the query, we update the validade field of the current user with the date and time added to 5 minutes, using MySQL's own NOW () and ADDTIME () (NOW () can be overridden by the internal variable CURRENT_TIMESTAMP if you prefer).

  • In 2 a query, we compared the validade field with the current time. If these 5 minutes have not yet passed, the comparison will be true, and the user included in the results.

27.03.2014 / 06:17
2

I think this only solves what you need, not the problem itself.

$timestamp=time(); 
$timeout=time()-300; 
$result = mysql_query("UPDATE novo_usuarios SET timestamp='$timestamp' WHERE ID='$id_user'");
$result = mysql_query("SELECT * FROM novo_usuarios WHERE ".intval(timestamp)." < ".intval($timeout));
    
27.03.2014 / 04:39