0
I need to subtract a date from another and show the result of this. However the result is sometimes not correct.
For example:
I subtract the date 2018-07-27 16:53 by 2018-07-27 16:51, the result would be 2 minutes, right? But, PHP returns me 31 days, 21 hours and 2 minutes.
The code is like this:
// $inicio vem do BD no formato YYYY-MM-DD HH:II:SS
$inicio = $dado_temp['inicio'];
$tempo_decorrido = time()-strtotime($inicio);
?>
<p class="termina"><?php echo date("j", $tempo_decorrido)."d ".date("H", $tempo_decorrido)."h ".date("i", $tempo_decorrido)."min" ;?></p>
// Ele deveria me retornar 0d 0h 02min, mas ele me retorna 31d 21h 02min. Se eu subtrair datas com um intervalo de tempo maior ele me retorna corretamente.
Vitor, I found an explanation of the "date_diff" function in the php documentation here: http://php.net/manual/en/function.date-diff.php
– Rodrigo Tognin
Legal Rodrigo. I made a difference, but the return is an array object. How I manipulate it?
– Vitor Leite
You can do it like this:
$diff = date_diff($data1, $data2);
echo 'Ano:' . $diff->y;
To catch the year. Use "m" for the month, "d" for the day, "h" for the hour, "i" for the minute, and "s" for the seconds.– Rodrigo Tognin
Thanks Rodrigo, it worked perfectly. :)
– Vitor Leite