how to compare hours and tell whether the establishment is closed or open and return a Json message

0

Someone help me ... Thanks. It's the following I have an Api with Php + Sql and it returns the data to me in Json. In my data has two fields openRestaurant and closeRestaurant, this data are registered in the bank in the format of hours, opening hours of an establishment and the time that closes the establishment how can I make that comparison of hours and return me only the message open or closed on my Json instead of returning my hours registered?

    
asked by anonymous 09.07.2016 / 07:47

2 answers

0
<?php
# dentro do seu while
$statusRestaurante = 'fechado';

# guarda o timestamp da hora que abre
$openTime  = new DateTime($result['openRestaurante']);
$openRestaurante = $openTime->getTimestamp();
# guarda o timestamp da hora que fecha
$closeTime = new DateTime($result['closeRestaurante']);
$closeRestaurante = $closeTime->getTimestamp();
# agora
$nowTime            =  new DateTime(date("H:i"));
$now = $nowTime->getTimestamp();
# se agora for maior ou igual a hora que abre e agora for menor ou igual a hora que fecha
if ( ($now >= $openRestaurante) && ($now <= $closeRestaurante) )
{
    $statusRestaurante = 'aberto';
}
# echo $statusRestaurante;
$out .= '"statusRestaurante": "'.$statusRestaurante.'",';
    
09.07.2016 / 16:20
0

You can perform date addition and subtraction operations using the php DateTime and DateInterval classes. link link

And then you can compare whether the current time precedes the opening time or if the closing time occurs, thus indicating that the restaurant is closed.

I hope I have helped.

    
09.07.2016 / 13:11