0
$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...

4

1 回答 1

2

示例如何以小时为单位获取日期/时间戳之间的差异:

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

更新:如果你想用前导零格式化输出数字,你可以使用sprintf()orstr_pad()函数。使用示例sprintf()

echo sprintf('%02d:%02d', $diff->days * 24 + $diff->h, $diff->i);
于 2013-10-07T16:20:40.657 回答