计算夜班时间记录的时差

时间:2013-07-31 08:13:46

标签: php time

我正在计算使用时间的夜班时间表的时差

让我说我有这些数据:

 $actual_in_time      = 6:45 PM        //date July 30, 2013
 $actual_out_timeout  = 7:00 AM        //date July 31, 2013

我必须计算应该将时间转换为整个时间的时差,因此

 $actual_in_time     = //(some code to convert 6:45 PM to 7:00 PM)
 $converted_in_time  = $actual_in_time;

现在这是我的代码:

 $actual_out_time   += 86400;
 $getInterval   = $actual_out_time - $converted_in_time;
 $hours     = round($getInterval/60/60, 2, PHP_ROUND_HALF_DOWN);
 $hours     = floor($hours);

我没有得到我想要的结果。你如何计算基准时间的时差?

2 个答案:

答案 0 :(得分:0)

您可以使用strtotime中的第二个参数来获取相对时间。

$start = strtotime($startTime);
$end = strtotime($endTime, $start);

echo ($end-$start)/3600;

答案 1 :(得分:0)

使用DateTime对象

$start =  new DateTime('2000-01-01 6:45 PM');
$end =  new DateTime('2000-01-01 7:00 AM');
if ($end<$start)$end->add(new DateInterval('P1D'));
$diff = date_diff($start,$end);
echo $diff->format('%h hours %i minutes');

如果您的结束时间少于开始时间,则添加1天。