我编写了以下代码来确定员工在某项任务上花费的时间:
$time1 = $row_TicketRS['OpenTime'];
$time2= $row_TicketRS['CloseTime'];
$t1=strtotime($time1);
$t2=strtotime($time2);
$end=strtotime(143000); //143000 is reference to 14:30
//$Hours =floor((($t2 - $t1)/60)/60);
$Hours = floor((($end- $t1)/60)/60);
echo $Hours.' Hours ';
上面的代码没有给我正确的时间。
例如,开始时间为 09:19:00,结束时间为 11:01:00,它给出的持续时间仅为 1 小时,这是错误的。正确的做法是什么?
最佳答案
您对 floor
的使用是您只有 1 小时的输入时间的原因。如果您将答案保持为 float ,这些输入将产生 1.7 小时。 floor
自动向下舍入到较低的整数值。查看http://php.net/manual/en/function.floor.php了解更多信息。
$t1 = strtotime('09:19:00');
$t2 = strtotime('11:01:00');
$hours = ($t2 - $t1)/3600; //$hours = 1.7
如果你想要更细粒度的时差,你可以充实它......
echo floor($hours) . ':' . ( ($hours-floor($hours)) * 60 ); // Outputs "1:42"
更新:
我刚刚注意到您对长耳朵的回答的评论。请再次检查我上面的评论,它们是正确的。输入值“09:11:00”和“09:33:00”会导致 0 小时(22 分钟)。
如果您输入这些值并得到 4 小时,则您的数学可能有小数错误。使用“09:11”到“09:33”,结果是 0.367 小时。如果您将 strtotime
结果除以 360 而不是除以 3600,您将得到 3.67 小时(或 4 小时,具体取决于您的舍入方法)的结果。
strtotime
converts your time to an int
value表示自 Unix 纪元以来的秒数。由于您将这两个值都转换为秒,然后将这些值相互减去,因此结果值是秒数。 1小时有3600秒。
关于php - 找出两次之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5177334/