php在时间开始和结束之间的累积时间

时间:2012-11-05 05:39:41

标签: php cumulative-sum

请有人解决我的问题。 我有mysql表,其中time_start'小时:分钟'和time_end'小时:分钟'我怎样才能获得time_start和time_end之间的累积持续时间。 感谢,

$sel = $db->query("SELECT * FROM logbook WHERE date = '$date' ");

while($sel_row = mysqli_fetch_array($sel))
{
  $duration = (strtotime($sel_row['time_end'])-strtotime($sel_row['time_start']))/(60*60);        
  $hour = floor($duration);
  $minute = ($duration - $hour)*60;
  echo $hour.":".$minute;
  ? HERE HOW CAN I CUMULATE THE $hour:$minute       
}

1 个答案:

答案 0 :(得分:0)

计算持续时间的总和

$duration = 0;
while($sel_row = mysqli_fetch_array($sel))
{
  $duration += (strtotime($sel_row['time_end'])-strtotime($sel_row['time_start']))/(60*60);        
}
$hour = floor($duration);
$minute = ($duration - $hour)*60;
echo $hour.":".$minute;

或者更好的是,让MySQL进行计算:

SELECT (SUM(time_end) - SUM(time_start)) / 3600 FROM logbook WHERE date = '$date';