$to = strtotime("2008-12-13 10:42:00");
$from = strtotime("2008-12-13 10:21:00");
echo round(abs($to_time - $from_time) / 60,2). " minute";
echo round(abs($to_time - $from_time) / 3600,2). " hours";
当我转换成分钟时它工作得很好,但是当转换成小时时它没有给出正确的输出。
答案 0 :(得分:0)
试试这个 资料来源:http://php.net/manual/en/datetime.diff.php
<?php
$datetime1 = new DateTime('2008-12-13 10:42:00');
$datetime2 = new DateTime('2008-12-13 10:21:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%h hour');
echo $interval->format('%R%i minute');
?>
答案 1 :(得分:0)
这应该这样做:
$from = new DateTime('2008-12-13 10:21:00');
$to = new DateTime('2008-12-13 10:42:00');
$diff = $to->diff($from);
$hours = $diff->h;//hours
$mins = $diff->i;//minutes
echo $hours.':'.$mins;
使用上面的$ diff是一个包含这些键的数组:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 0
[i] => 21
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 1
[days] => 0
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
答案 2 :(得分:0)
我不知道&#34;正确的输出是什么&#34;你期待的。
21 minute
0.35 hours
差异是21分钟,所以0.35h是正确的。
无论如何,您的代码错了,您使用的是$from_time
和$to_time
,而不是$to
和$from