Problem when comparing hours

Asked

Viewed 2,142 times

3

When comparing timetables I have in the registered database the opening and closing hours of an establishment. I’m having a problem comparing the hours to see if the establishment is open or closed, it’s always appearing to me as open when it’s past time.

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

inserir a descrição da imagem aqui

  • Guy include this: date_default_timezone_set('America/Sao_paulo'); Depending on your location... try to give one echo In your present hour you’re hitting it right.

  • I personally prefer to work with gmt (gmdate(...)) and convert the hours according to "the region" or user preference. If you use date() you may have many problems with time zones and "poorly configured" servers. I have a VPS service and there was some problem in the upgrade, I just had problem with several things, including the schedule. Correct me I’ve said something wrong.

2 answers

2


Dude, analyze 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';
}
  • It worked right thanks for all the help

  • It is true, I had not paid attention to these data.

2

You need both conditions to be met:

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

The && in place of || ensures this. In your code, one of the conditions is enough to enter the if, causing more events to display than you would like.

  • I had already tested this way but now so it always remains me as closed

  • What is the format of dates in your bank?

  • Format is like Varchar(255)

  • I am making time in this H:i format

  • I put an image with the structure of the hourly database

  • The server is likely to work in UTC, but your hours are in another zone.

  • 1

    Use echo date_default_timezone_get(); to view the server time zone.

  • Fuso Horario Europe/Lisbon

  • I was wrong, how everything uses strtotime, everything stays in the same time zone. It should work, see only: http://ideone.com/y71gaf (change the start/end times to test)

  • Really there in the example works well I do not know why it will be I also thought everything was okay until I decided to here to see if we find the cat

  • But here I’ve been watching and I can’t find any mistake keeps staying as closed

Show 6 more comments

Browser other questions tagged

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