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 :)
Take a look at the Datetime class of
php
– Leonardo
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.
– Bacco
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 (withtempo + (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.– Leonardo
Time and date functions in PHP give a thousand to zero in that Datetime crap in both performance and simplicity :)
– Bacco
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) thedate
andtime
are a baile kk– Leonardo
@lvcs date tb makes :)
echo date('w');
– Bacco
That one I didn’t know kk
– Leonardo