Comparison and calculation of time and date

Asked

Viewed 150 times

1

Well I retrieve the date and time of a record like this:

$sql = $conexao->query(select data, hora from pedido);
$resultado = mysqli_fetch_object($sql);

$resultado->data;
$resultado->hora;

I need to add 5 hours and check if the date is longer than every current time.

How can I make this calculation is verification?

I wish I had something like that

if ((data e hora da tabela com + 5 horas) => (data e hora atual)) {
 echo "o pedido já foi finalizado a mais de 5 horas";
}
  • Good with that I can add hours, but I don’t know how to buy.

  • Just compare the return of strtotime.

1 answer

0

In case of comparison you can use something like this:

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

Source in the SOPT itself

If you want to validate date and time you can also use this answer from SOPT: Validate date and time

  • I have to use the date too, if I use only the time can go wrong the other day.

  • @Hugoborges edited the answer with another reference.

Browser other questions tagged

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