计算工作总时数

时间:2016-08-01 08:29:43

标签: php

我有一个数组,其中包含当天雇主的日期和进出。通常每天只有4行(In -> Out (lunch) -> In -> Out)

我想按照进/出的方式将所有工作时间计算在一起并显示给雇主。

所以我的日常工作结构如下:

Day '2016-07-01' -> Events (4) -> (0) => ('in', '09:00')
                                  (1) => ('out', '13:00')
                                  (2) => ('in', '14:00')
                                  (3) => ('out', '17:00')

等等..

上面的例子中,下面的代码是打印6.90,这是不正确的,正确的结果应该是8小时。

$total_minutes = 0;
$total_hours   = 0;

foreach($listOfDays as $day)
{
   foreach($day->events as $key => $event)
   {
      // It's always 'in'
      if(($key % 2 == 0) || $key == 0)
      {
         $hour_in  = new DateTime($event->hour);
         $hour_out = null;

         if(isset($day->events[$key + 1]->hour))
           $hour_out = new DateTime($day->events[$key + 1]->hour);

         if($hour_out != null)
         {
            $total_hours += $hour_out->diff($hour_in)->format('%H');
            $total_minutes += $hour_out->diff($hour_in)->format('%i');
         } 
      }
      // It's always 'out'
      else
      {
        $hour_in  = new DateTime($day->events[$key - 1]->hour);
        $hour_out = new DateTime($event->hour);

        $total_hours += $hour_out->diff($hour_in)->format('%H');
        $total_minutes += $hour_out->diff($hour_in)->format('%i');
      }
   }
}

$total_hours_worked = $total_hours . '.' . $total_minutes;
print_r($total_hours_worked);

2 个答案:

答案 0 :(得分:0)

<强>解决

由于key % 2始终是out移动,因此我不需要in移动的条件,因为out已经验证。

if($key % 2 != 0)
{
    $hour_in  = null;
    $hour_out = $event->hour;

    if(isset($day->events[$key - 1]->hour))
        $hour_in = $day->events[$key - 1]->hour;

    if($hour_in != null)
        $diff += (strtotime($hour_out) - strtotime($hour_in));
}

在循环结束时:

$total              = $diff / 60;
$total_hours        = floor($total / 60);
$total_minutes      = floor($total % 60);
$total_hours_worked = sprintf('%02dh%02d', $total_hours, $total_minutes);

基于PHP Calculate total time

答案 1 :(得分:0)

要计算两次之间的时间范围,可以使用以下代码进行检查

function calculateTotalTime($fromtime_temp, $totime_temp){
    $fromtime = $fromtime_temp;
    $totime = $totime_temp;

    $totalSec = (strtotime($totime) - strtotime($fromtime));
    $totalMin = floor($totalSec/60);
    $totalSec = $totalSec%60;

    $totalHour = floor($totalMin/60);
    $totalMin = $totalMin%60;

    $totalDay = floor($totalHour/24);
    $totalHour = $totalHour%24;


    $totalMonth = floor($totalDay/30);
    $totalDay = $totalDay%30;

    $totalWeek = floor($totalDay/7);
    $totalDay = $totalDay%7;

    $totalYear = floor($totalMonth/12);
    $totalMonth = $totalMonth%12;


    $returnValue = "";
    if($totalYear > 0){
        if ($totalYear == 1) {
            $returnValue .= $totalYear.'year '; 
        }else{
            $returnValue .= $totalYear.'years ';
        }
    }

    if ( $totalMonth > 0 ) {
        if ($totalMonth == 1) {
            $returnValue .= $totalMonth.'month ';   
        }else{
            $returnValue .= $totalMonth.'months ';
        }
    }

    if ( $totalWeek > 0 ) {
        if ($totalWeek == 1) {
            $returnValue .= $totalWeek.'week '; 
        }else{
            $returnValue .= $totalWeek.'weeks ';
        }
    }

    if ( $totalDay > 0 ) {
        if ($totalDay == 1) {
            $returnValue .= $totalDay.'day ';   
        }else{
            $returnValue .= $totalDay.'days ';
        }
    }

    if ( $totalHour > 0 ) {
        if ($totalHour == 1) {
            $returnValue .= $totalHour.'hour '; 
        }else{
            $returnValue .= $totalHour.'hours ';
        }
    }

    if ( $totalMin > 0 ) {
        if ($totalMin == 1) {
            $returnValue .= $totalMin.'min ';   
        }else{
            $returnValue .= $totalMin.'mins ';
        }
    }

    if ( $totalSec > 0 ) {
        if ($totalSec == 1) {
            $returnValue .= $totalSec.'sec ';   
        }else{
            $returnValue .= $totalSec.'secs ';
        }
    }

    return $returnValue;
}

它将返回正确格式的字符串值以直接在客户端站点上显示,并且您也可以更改格式