我只用时间来计算夜班的时差。
假设我有这样的数据:
$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);我没有得到我想要的结果。当基数就是时间时,如何计算时间差?
发布于 2013-07-31 16:40:06
使用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天。
发布于 2013-07-31 16:24:27
您可以使用strtotime中的第二个参数来获取相对时间。
$start = strtotime($startTime);
$end = strtotime($endTime, $start);
echo ($end-$start)/3600;https://stackoverflow.com/questions/17965013
复制相似问题