Time Comparison in Approach

Asked

Viewed 64 times

-1

How can I make a script for if current time is approaching the scheduled time to do something

  Exemplo

  date_default_timezone_set('America/Sao_Paulo');
  $hora_atual = date('H:i'); //11:45 Hora Atual      
  $hora_marcada              // 12:00 hora marcada vem do banco 

  se horario atual estiver faltando <= 15 minutos para a hora marcada{

  exemp .echo falta 15 minutos para saida  
  }
  • 1

    Take a look at the Datetime class of php

  • It depends on all the other code you didn’t put in the question. Which variables are that time, where the script is, how it will run, etc. - Usually it’s just a small IF with a conversion in minutes or seconds, it depends on how you store the date.

  • 1

    Or you can use the time() even, it returns the current timestamp, hence Voce stores in a variable and start and adds another 15 minutes (with tempo + (15*60)) and stores in the output variable, after that is just take the output - the input and multiply by 60 to have the number of minutes remaining.

  • 1

    Time and date functions in PHP give a thousand to zero in that Datetime crap in both performance and simplicity :)

  • I agree kk the advantage o DateTime is that it makes some automatic calculations, such as weekdays, etc. But for simpler questions (as most of the times) the date and time are a baile kk

  • 1

    @lvcs date tb makes :) echo date('w');

  • That one I didn’t know kk

Show 2 more comments

1 answer

1

I wouldn’t answer because I’m on my cell phone. But I decided to answer.

You can use the class DateTime (recommended), or the time():

<?php
    $inicio = time(); #pega o tempo atual em segundos (timestamp)
    $fim = $inicio + (15 * 60); #acrescenta 15 minutos ao tempo anterior

    $x = $fim - $inicio; #pega quanto falta em segundos
    $x = $x / 60; # transforma em minutos 

    echo "Faltam {$x} minutos";

    #pode comparar os valores, por exemplo:
    echo ($fim < time()) ? "Foi liberado" : "Ainda nao esta liberado";

     #olho se o numero de segundos do fim é menor que o tempo atual, se sim quer dizer que o tempo em fim ja se tornou passado

Of course, the example always spoke that there are 15 minutes left, in order to work normally the variables start and end should be stored in cookies or session.

Should work, not tested, was in mind kk

Edited

Now yes, with the question more or less clear, so resolves:

<?php
    date_default_timezone_set('America/Sao_Paulo');

    $fim = '16:30:00';
    $inicio = date("H:i:s", time());
    echo "inicio: {$inicio}<br>";
    echo "fim: {$fim}<br>";

    $arrayFim = explode(":", $fim);
    $arrayInicio = explode(":", $inicio);

    $horas = $arrayFim[0] - $arrayInicio[0];
    $minutos = $arrayFim[1] - $arrayInicio[1];
    $segundos = $arrayFim[2] - $arrayInicio[2];
    if($segundos < 0)
    {
        if($minutos > 0)
        {
            $minutos --;
            $segundos = 60 - $arrayInicio[2];
        }
    }
    echo "<br>horas: {$horas}<br>";
    echo "minutos: {$minutos}<br>";
    echo "segundos: {$segundos}<br>";

    if($horas <= 0 and $minutos <= 0 and $segundos <= 0)
        echo "<br>Expirado";
    else
        echo "<br>Nao expirou";

Next time, make it clear what the doubt is :)

  • I think there is something wrong or I do not know how to use. always returning me 54000 minitos for any time you put

  • I got confused, yeah $x = $x / 60;

  • Another confusion was in the comparison, it is not > and yes <

  • something still doesn’t work

  • I put here just for demonstration I know that on and for PHP https://jsfiddle.net/a5suy5xa/

  • So it won’t work, time is marked by seconds, study about timestamp. I answered what the question was asking, which is about hours comparison, you did not give enough detail, edit the question or make a new.

  • But I haven’t changed at all. I only put time zone, to take the current time of the region and my question is what I need, and I did not put more details because I accepted any example that works

  • I edited the answer with another code, next be clearer in doubt.

  • OK almost that... and the question of 15 minutes? if I understood right You put... if the time is equal to the current --> Expired otherwise it did not expire

  • only compare the variable minutes, if it is equal to 15 or less you say that less than 15 minutes '-'

Show 5 more comments

Browser other questions tagged

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