计算总小时数,最佳解决方案


Calculate total hours, best solution?

我正在制作一个包含条目的表,其中包含签入,签出和描述。checkin和checkout都是datetime字段。

我想计算完成一个项目的总时间。这是我在1次签出和一次签入之间的计算:

{? $checkin = new DateTime($hour->checkin) ?}
{? $checkout = new DateTime($hour->checkout) ?}
@if($hour->checkout == '0000-00-00 00:00:00')
    {? $checkout = new DateTime() ?}
@endif
{? $diff = $checkout->diff($checkin) ?}
{? $hours = $diff->format('%H:%I') ?}

现在,我计算总秒数:

$totaltime += strtotime("January 1, 1970 " . $hours . ":00")

然后在所有内容显示之后,我计算HOURS的数量:

public static function format_time($seconds,$separator=':', $format = "%02d%s%02d") // t = seconds, f = separator 
    {
        return sprintf($format, floor($seconds/3600), $separator, ($seconds/60)%60, $separator, $seconds%60);
    }

然而,当我这样做的时候:-

strtotime("January 1, 1970 00:40:00")

得到一个负整型。我做错了什么?

<?php
$checkin[0] = "2013-03-09 10:00:01";
$checkout[0] = "2013-03-12 10:55:15";
$checkin[1] = "2013-03-15 10:00:01";
$checkout[1] = "2013-03-15 10:55:15";
$checkin[2] = "2013-04-09 10:00:01";
$checkout[2] = "2013-04-15 10:55:15";
$diffSeconds = 0;
for($i = 0; $i < count($checkin)-1; $i++){
  $diffSeconds += strtotime($checkout[$i]) - strtotime($checkin[$i]);
}
$hours = floor($diffSeconds/3600);
echo $hours.' hours';
?>