user2010470
user2010470

Reputation: 21

Change days to hours with date_diff

$time = Whatever i want
$future_date = new DateTime(date('r',strtotime($time)));
$time_now = time();
$now = new DateTime(date('r', $time_now));
interval = date_diff($future_date, $now);
echo $interval->format('%H:%I');

When i run this... i get an output WITH days, you just cant see them. How can i roll the days into the hours (like to have 35 hours for example)?

This is part of a longer script im writing, im aware there are different ways of doing this....

EDIT

Using the answers below, i've come up with this:

$time = '2013-10-8 11:10:00';
$future_date = new DateTime($time);
$now = new DateTime();
$interval = date_diff($future_date, $now);
$text = $interval->days * 24 + $interval->h . ':' . $interval->i; 

I'm trying to output the hours and minutes in this format (00:00), WITH leading zeros. Now out of my depth...

Upvotes: 0

Views: 159

Answers (1)

Glavić
Glavić

Reputation: 43552

Example how to get difference between dates/timestamps in hours:

$future = new DateTime('@1383609600');
$now = new DateTime;
$diff = $now->diff($future);
echo $diff->days * 24 + $diff->h;

Update: if you wish to format output numbers with leading zeros, you can use sprintf() or str_pad() function. Example of sprintf() use:

echo sprintf('%02d:%02d', $diff->days * 24 + $diff->h, $diff->i);

Upvotes: 2

Related Questions