php count hours remaining [duplicate]

0

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?

    
asked by anonymous 01.02.2018 / 18:02

2 answers

1

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);

Demo

    
01.02.2018 / 18:34
0

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');
    
01.02.2018 / 18:43