PHP计算总小时数无法获得准确答案

时间:2019-02-09 06:25:45

标签: php date datetime

我正在尝试根据他们inbreakresumeout的时间来计算一个人的总数。

我有这些数据:

$data[0]['type'] = 'in';
$data[0]['created_at'] = '2019-02-01 08:01:52';

$data[1]['type'] = 'break';
$data[1]['created_at'] = '2019-02-01 12:00:40';

$data[2]['type'] = 'resume';
$data[2]['created_at'] = '2019-02-01 13:00:39';

$data[3]['type'] = 'break';
$data[3]['created_at'] = '2019-02-01 14:29:21';

$data[4]['type'] = 'resume';
$data[4]['created_at'] = '2019-02-01 14:29:50';

$data[5]['type'] = 'break';
$data[5]['created_at'] = '2019-02-01 14:29:53';

$data[6]['type'] = 'resume';
$data[6]['created_at'] = '2019-02-01 14:30:00';

$data[7]['type'] = 'break';
$data[7]['created_at'] = '2019-02-01 14:30:09';

$data[8]['type'] = 'resume';
$data[8]['created_at'] = '2019-02-01 14:30:12';

$data[9]['type'] = 'out';
$data[9]['created_at'] = '2019-02-01 18:01:51';

$hours = 0;
$minutes = 0;
foreach($data as $key => $d){
    $d['created_at'] = str_replace(" ","T",$d['created_at']);
    if($d['type'] == 'in' || $d['type'] == 'resume'){
        $workingTime = new DateTime($d['created_at']);
    }

    if($d['type'] == 'break' || $d['type'] == 'out'){
        $date2 = new DateTime($d['created_at']);
        $diff = $date2->diff($workingTime);
        $tempHours = $diff->h;

        $tempHours = $tempHours + ($diff->days*24);
        $hours += $tempHours;

        $tempMin = $diff->i;
        $minutes  += $tempMin;
    }
}

if($minutes == 60){
    $hours += 1;
    echo $hours. " hours and 00 minutes";
}else if($minutes > 60){
    $hours = $hours + floor($minutes/60);
    $minutes = $minutes % 60;
    echo $hours. " hours and ".$minutes." minutes";
}else{
    echo $hours. " hours and ".$minutes." minutes";
}

我得到的总数是:8 hours and 57 minutes正确答案应该是 08 hour and 59 minutes and 21 seconds

我认为我没有得到正确答案的原因是因为在我的代码中,我没有在计算中包括秒数。

有人可以通过在计算中包括秒来帮助我获得正确答案。

您的帮助将不胜感激!谢谢。

顺便说一下,由于以下网站,我知道正确答案是08 hour and 59 minutes and 21 seconds的原因:https://www.calculator.net/time-calculator.html

我得到了所有这些差异的总和:

$a = break - in
$b = break - resume
$c = out - resume
Then: $total_time = $a + $b + $c

让我知道是否不清楚。谢谢。

1 个答案:

答案 0 :(得分:2)

此代码将执行您想要的操作。它处理数组的每个元素,并在看到in时设置工作的开始和结束时间;每次看到inresume时,都要设置一个时期开始;并在每次看到outbreak时将周期长度添加到一天的工作时间中。最后,我们将一天的开始时间与我们创建的所有时间段相加得出的时间差,即整天的工作时间。

foreach ($data as $event) {
    $this_time = new DateTime($event['created_at']);
    switch ($event['type']) {
        case 'in':
            $work_start_time = clone $this_time;
            $work_end_time = clone $this_time;
        case 'resume':
            $start_time = $this_time;
            break;
        case 'break':
        case 'out':
            $work_end_time->add($start_time->diff($this_time));
            break;
    }
}
$working_hours = $work_start_time->diff($work_end_time);
echo $working_hours->format('%H hours, %i minutes, %s seconds');

输出:

08 hours, 59 minutes, 21 seconds

注意

如果轮班时间可能超过一天,则需要将最后一行更改为此:

echo $working_hours->format('%a days, %H hours, %i minutes, %s seconds');

或者如果您只需要几个小时而不是几天,请使用以下方法:

echo $working_hours->days*24+$working_hours->h . $working_hours->format(' hours, %i minutes, %s seconds');

Demo on 3v4l.org