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

Asked

Viewed 543 times

0

Help me... Thank you. This is the following I have an Api with Php + Sql and returns the data in Json. In my data you have two fields openRestaurante and closeRestaurante, opening hours of an establishment and closing time of the establishment how can I make this comparison of hours and return me only the open or closed message in my Json instead of returning me the registered hours?

inserir a descrição da imagem aqui

2 answers

0

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.'",';
  • 1

    Thank you so much for the answers, I wanted to ask if I put these lines of code inside my while of my code it would return me already open or closed ? Thank you

Browser other questions tagged

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