计算总工作小时数


Calculate total hours worked

我有一个数组,其中包含日期和雇主在一天中的进出。通常每天只有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);

已解决

由于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计算总时间

对于计算两次之间的时间框架,您可以使用以下代码进行检查

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;
}

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