Problem when comparing hours

3

In the comparison of schedules I have in the registered database the opening and closing times of an establishment. I'm having a problem when comparing the time to see if the establishment is open or closed, it always appears to me as open when the time has passed.

<?php
$horario = strtotime(date('H:i'));
$result_horario_estado=mysql_query("select * from horarios where id_mae='".$row->id."'");
$row_horario_estado=mysql_fetch_array($result_horario_estado);

$abertura   = strtotime($row_horario_estado['horario_abertura']);
$fechamento = strtotime($row_horario_estado['horario_fechamento']);

if($horario >= $abertura || $horario <= $fechamento) {
?>
    <div style="margin:-3px 0px 5px 0px; width:257px; font-family:Arial, Helvetica, sans-serif; color:#0C3; font-size:13px;">Aberto</div>
<?php
} else {
?>
    <div style="margin:-3px 0px 5px 0px; width:257px; font-family:Arial, Helvetica, sans-serif; color:#F00; font-size:13px;">Fechado Agora</div>
<?php
}
?>

    
asked by anonymous 16.02.2015 / 01:09

2 answers

2

Dude, look at your code. You open at 14 and close at 2. You have to consider this. 14 is greater than 2.

<?php
$horario = strtotime('1:00');
$abertura = strtotime('22:00');
$fechamento = strtotime('7:00');

if ((
        $abertura < $fechamento && $horario >= $abertura && $horario <= $fechamento
        ) || (
        $abertura > $fechamento && ($horario >= $abertura || $horario <= $fechamento)
        )) {
    echo 'aberto';
} else {
    echo 'fechado';
}
    
17.02.2015 / 10:09
2

You need both of these conditions to be met:

if($horario >= $abertura && $horario <= $fechamento){
            //  VEJA --- ^^

&& instead of || ensures this. In your code, just one of the conditions to enter if , causing more events to show up than you'd like.

    
16.02.2015 / 01:13