4

時間だけを使って夜勤の時差を計算しています

私はこのデータを持っているとしましょう:

 $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);

望んでいた結果が得られません。基準が時間だけの時差をどのように計算しますか?

4

2 に答える 2

1

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-31T08:40:05.960 に答える
0

strtotime の 2 番目のパラメーターを使用して、相対時間を取得できます。

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

echo ($end-$start)/3600;
于 2013-07-31T08:24:27.450 に答える