How to check if the user is logged in 1 hour ago or logged in

Asked

Viewed 82 times

1

I’m creating a system to check if the user entered the site an hour ago or earlier. There’s a column in the database that speaks last time you logged on Last_login(int4) the date has to be in this format ymdHm(1703221703).

My problem is that I don’t know how I can verify this, whether it logged in 1 hour ago or earlier, or whether it is logged in.

My code I’ve made so far:

    public function check_time(){
    $date = new \DateTime();
    //echo $date->format('ymdHm');
    $conexao = new Config;
    try{
        $conect = $conexao->getConn();
        $prepare = $conect->prepare("SELECT * FROM accounts WHERE login = ?");
        $prepare->bindvalue(1, $_SESSION['username']);
        $prepare->execute();
        $dados = $prepare->fetch(PDO::FETCH_ASSOC);
        if ($prepare->rowCount() >= 1){
            //return $dados['last_login'];
            if($dados['last_login'] == 

        }



    }catch(PDOException $e){
        echo "Erro: ".$e->getMessage();
    }
}
  • Welcome to the Guilherme site. Do you want to check if the user logged in more than 1 hour ago? What if he did but navigated between several pages? Do you want this to be considered as well? As you are new, enjoy and take a look at tour.

  • Don’t just check if he logged on an hour ago or earlier.

1 answer

1


I’ll assume you know how to make the bank appointment and get the return.

Whereas you saved the bank’s return on the variable $retorno:

$zonaTemporal = new \DateTimeZone('America/Sao_Paulo');

$retorno = '201703221525';

$logouEm = \DateTime::createFromFormat('YmdHi', $retorno, $zonaTemporal);

if($logouEm == null) {
  /* Data Inválida no Banco, faz algum tratamento */
}

$intervalo = new \DateInterval('PT1H');

$agora = new \DateTime('now', $zonaTemporal);

$logouEm = $logouEm->add($intervalo);

echo 'Fez o último login em:<br>' . $logouEm->format('Y-m-d H:i:s');
echo '<br>';

echo 'Agora é:<br>' . $agora->format('Y-m-d H:i:s');
echo '<br><br>';


if($logouEm > $agora) {
    echo 'Logou a mais de uma hora';
} else {
    echo 'Ainda não deu uma hora';
}

Note: In this example I am assuming that the hours are saved with the Brazilian temporal zone. This may not be your case. I recommend you change the time zone to suit the way you’re saving. If you don’t know which time zone is saved in the database, it is likely that it is the default value of your php.ini which is usually the same time zone as the computer running php.

  • this add($range); function is what ? Call to a Member Function add() on Boolean

  • @William, this function is part of the object Datetime php. If you received this error, it is a sign that your date is in another format, or even null. I updated the example to get closer to how it is in your database.

  • thanks just changed the format worked :) Last logged in: 2017-03-22 18:00:00 Now it’s: 2017-03-22 17:12:44

Browser other questions tagged

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