Users Active on the site

0

My intention is to create a system of online people on my site, every 30s I send a request in ajax to my server and I set the ip and the time of the visit, my doubt is the following in my bank I record in the field time (03:29) using the date function how can I compare this recorded time with my bank to the current time and know if 30 seconds have already passed? because so when it passes 30 seconds I delete the active visits database

    
asked by anonymous 06.01.2018 / 06:31

1 answer

0
  

Using the date function How can I compare this time recorded with my bank to the current time and know if 30 seconds have already passed?

Answer this question, come on.

To do this you do not need to use the date function (although it is possible), but in this case it is best to only use the strtotime or strftime function, you can convert the date to seconds or milliseconds and subtracting the new data from 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 , you can also:

<?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:

<?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() );
    
06.01.2018 / 06:59