What is the best way to calculate the remaining hours of a date? Example:
$date = 2018-02-02;
$calcular_restante = ....;
X Days and X hours to arrive per day.
What is the best way to do this?
What is the best way to calculate the remaining hours of a date? Example:
$date = 2018-02-02;
$calcular_restante = ....;
X Days and X hours to arrive per day.
What is the best way to do this?
You can use the DateTime and DateInterval .
The class DateTime has a method ( DateTime::diff
) that, passing another date, it will calculate the difference and will return you a DateInterval telling you how much you miss in years, months, days, hours, minutes and seconds to get to a certain nothing.
Example:
<?php
$dataInicial = new DateTime();
$dataFinal = new DateTime("2018-07-13");
$diferenca = $dataInicial->diff($dataFinal);
echo sprintf("Faltam %d anos, %d meses, %d dias, %d horas, %d minutos e %s segundos para {$dataFinal->format('d/m/Y')}",
$diferenca->y,
$diferenca->m,
$diferenca->d,
$diferenca->h,
$diferenca->m,
$diferenca->s);
Hello, try this way, you can adapt the code to get the desired result.
<?php
$data1 = new DateTime();
$data2 = new DateTime('2018-08-20 00:00:00');
echo $data2->diff($data1)->format('Faltam %Y Anos %m Mês, %d dias e %h horas %i minutos');