怎么得到PHP总时间?

时间:2009-12-16 13:02:17

标签: php time

我用PHP创建了工作总时间程序 - 输入时间 - 1.30,2.10,1.40并获得输出时间 - 4.80(8小时)。但我需要输出时间 - 5.20(8.40小时)。 注意:1.30 + 2.10 + 1.40 = 4.80(8小时),但我需要5.20(8.40小时)。请帮帮我......

4 个答案:

答案 0 :(得分:2)

1.30 + 2.10 + 1.40错了。应该是:

<(>(1 * 60)+ 30)+((2 * 60)+ 10)+((1 * 60)+40)= 320(分钟)

320分钟= 5小时20分钟。

答案 1 :(得分:0)

您需要分别跟踪分钟和秒钟:

$minutes = array();
$seconds = array();
foreach ($times as $time) {
    $parts = explode('.', $time);
    $minutes[] = $time[0];
    $seconds[] = $time[1];
}
$total_minutes = array_sum($minutes);
$total_seconds = array_sum($seconds);
while ($total_seconds > 60) {
    $total_minutes++;
    $total_seconds -= 60;
}
echo $total_minutes . ' minutes and ' . $total_seconds . ' seconds';

答案 2 :(得分:0)

摘自PHP网站:

function AddTime ($oldTime, $TimeToAdd) { 
  $pieces = split(':', $oldTime); 
  $hours=$pieces[0]; 
  $hours=str_replace("00","12",$hours); 
  $minutes=$pieces[1]; 
  $seconds=$pieces[2]; 
  $oldTime=$hours.":".$minutes.":".$seconds; 

  $pieces = split(':', $TimeToAdd); 
  $hours=$pieces[0]; 
  $hours=str_replace("00","12",$hours); 
  $minutes=$pieces[1]; 
  $seconds=$pieces[2]; 

  $str = $minutes." minute ".$seconds." second" ; 
  $str = "01/01/2000 ".$oldTime." am + ".$hours." hour ".$minutes." minute ".$seconds." second" ; 

  if (($timestamp = strtotime($str)) === false) { 
    return false; 
  } else { 
    $sum = date('h:i:s', $timestamp); 
    $pieces = split(':', $sum); 
    $hours = $pieces[0]; 
    $hours = str_replace("12", "00", $hours); 
    $minutes = $pieces[1]; 
    $seconds = $pieces[2]; 
    $sum = $hours.":".$minutes.":".$seconds;
    return $sum; 
  } 
} 

$firstTime  = "00:03:12"; 
$secondTime = "02:04:34"; 

$sum=AddTime($firstTime, $secondTime); 

if($sum != false) { 
  echo $firstTime." + ".$secondTime." = ".$sum; 
} else { 
  echo "failed"; 
} 

输出:

00:03:12 + 02:04:34 = 02:07:46

答案 3 :(得分:0)

对于每个号码(下面用$t表示),您可以这样做:

// start with $total=0
$hours = floor($t); // 1.10 -> 1 hr
$minutes = ($t - $hours) * 100; // 1.10 -> 10 mins
$total += ($hours * 60) + $minutes;

这会给你总分钟数。要分别获得小时/分钟,请执行以下操作:

$total_mins = $total % 60; // 130 -> 10 mins
$total_hours = ($total - $total_mins) / 60; // 130 -> 2 hrs