I'm calculating the difference between two dates (the current date and a date in the database). I want to display the difference in days and hours between the dates, but when the day-difference is 0 I don't want to display it. This is my code:
<?php
$to_time = new \DateTime($database_time);
$from_time = new \DateTime();
echo $from_time->diff($to_time)->format("%d %H");
?>
Output should be, for example:
> 50 days and 4 hours
> 1 day and 7 hours //and not 1 dayS
> 6 hours //and not 0 days and 6 hours
There are two issues with my own code: the first one is that it always display the number of days. The second one is that it is just a format. For example when the difference is 2 months, 6 days and 2 hours it displays: 6 days and 2 hours. But it should be 68 days and 2 hours (because of the two months).
Can't get it work with other codes I found. Thanks in advance!