Compare current time with time recorded in the database?

1

I have an hour logged in the database and would like to compare it with the current time. How should I do it with PHP ?

The reason for this is that I needed to select a value that was available at a certain time.

The fields I have are:

  • start_time in time ()
  • End_time in time ()

And I needed you to select one of the values from the database that was within the time.

    
asked by anonymous 14.05.2018 / 19:09

2 answers

2

I did it in a way that I did on my own.

First I looped through all the data in the table and compared the hours.

$select = mysqli_query($db, "SELECT * FROM programas");
while($mos2=mysqli_fetch_assoc($select)){
    $str_inicio=strtotime($mos2['hora_inicio']);
    $str_fim=strtotime($mos2['hora_fim']);
    $str_atual=strtotime($hora_toda);
    $diferenca=$str_fim-$str_atual;
    if($str_atual > $str_inicio && $str_atual < $str_fim){
        $variavel = $mos2['id'];
    }
}

With this, it will return what has the id that I want to submit to ensure.

$select2=mysqli_query($db, "SELECT * FROM programas where id = '".$variavel."'");
if(mysqli_num_rows($select2)){
$mos=mysqli_fetch_assoc($select2);

$radio = $mos['hora_inicio']." - ".$mos['hora_fim'];

echo $mos['img'].",".$mos['nome'].",".$radio.",".str_replace(" ", "_", $mos['nome']);
}

Note: The last echo is used later in the code in the code so no explanation is needed.

    
14.05.2018 / 19:44
1

Instead of $inip and $fimp you should put the schedule that is in your database:

$QUERY = SELECT * FROM BANCO.TABELA;
$EXECUTE_QUERY = mysqli_query($conexao, $QUERY);
$DATA = mysqli_fecth_array($EXECUTE_QUERY);
$inip = $DATA['hora_inicio'];
$fimp = $DATA['hora_fim'];

$startP = $inip;
$endP   = $fimp;
$now    = time();//Hora atual

if($startP <= $now && $now <= $endP){
    echo "Dentro do horario";
} else {
    echo "Fora do horario";
}
    
14.05.2018 / 19:20