Active users on the site

Asked

Viewed 40 times

0

my intention is to create a system of people onlines on my site, every 30s I send a request in ajax to my server and mark the ip and time of the visit, my doubt is the following in my bank I record in the field hour ( 03:29 ) using the date function how can I be comparing this recorded time with my bank to the current time and know if it’s been 30 seconds ? because then when it passes 30 seconds I delete from the database the active visits

  • Maybe this will help you Behold

1 answer

0

using the date function how can I be comparing this recorded time with my bank to the current time and know if it has been 30 seconds ?

Answer that question, come on.

To do this you do not need to use the function date (although it is possible), but in this case it is better to use only the function strtotime or strftime, you can convert the date to seconds or milliseconds and overwrite the new data with the old data.

<?php

/* Captura o tempo em segundos desde 1970 */
$date1 = strtotime("2018-01-06 02:43:00"); //Output: 1515206580
$date2 = strtotime("2018-01-06 02:43:32"); //Output: 1515206612

/* Utilize strtotime("+30 seconds") para capturar a data atual + 30 segundos */

/* Substrai as datas e verifica se $data é maior que 30 (Segundos) */
if ( ($date2 - $date1) > 30 ) {
    echo "Já se passaram mais de 30 segundos";
}

If you want to do with DateTime and DateInterval, it is also possible:

<?php

/* Instancia o objeto com as respectivas datas */
$date1 = new DateTime("2018-01-06 02:43:00");
$date2 = new DateTime("2018-01-06 02:43:32");

/* Captura a diferença entre a $data2 e $data1 */
$interval = $date2->diff($date1);

/* Captura a diferença em segundos e verifica se é maior que 30 */
if ($interval->format("%s") > 30) {
    echo "Já se passaram mais de 30 segundos";
}

Or else:

<?php

/* Instancia o objeto com uma data. Para capturar a data/hora atual, deixe em branco ou passe o valor "now" */
$date1 = new DateTime("2018-01-06 02:43:00");

/* Adiciona 30 segundos a data anterior */
$date1->add(new DateInterval("PT30S"));

/* Verifica se a data atual é maior que a data anterior, somado os 30 segundos. */
var_dump( $date1->format("U") < time() );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.