日期和时间差显示30分钟为5

时间:2015-04-29 10:42:34

标签: php

我试图使用这个PHP代码比较两个日期和时间

<?php $A = "2013-01-10 10:30:00";
    $B = "2013-01-10 12:00:00";
    echo str_replace('-','',(strtotime($A) - strtotime($B))/(60*60));
    ?>

代码工作正常,但它显示30分钟为5所以我无法用db值检查这个任何想法来解决这个问题

2 个答案:

答案 0 :(得分:2)

strtotime()就可以计算出差异。无需使用str_replace()。试试 -

echo (strtotime($B) - strtotime($A))/60;

将返回minutes中的差异,即。 90。如果你想要1.30,那么必须做一些基本的计算。 -

$mins = (strtotime($B) - strtotime($A))/60;
if ($mins > 60) {
    echo ((int)($mins/60)).'.'.($mins%60);
} else {
    echo $mins;
}

答案 1 :(得分:1)

我已根据您的评论更新了我的答案,以获得您需要的小时数(即:1.5):

 $A = "2013-01-10 10:30:00";
 $B = "2013-01-10 12:00:00";

echo (strtotime($B) - strtotime($A))/3600;

输出:

1.5

演示: http://ideone.com/ovEr0F