如何从mysql中找到时间总和

时间:2012-10-22 03:35:02

标签: php

你好,我要从我的sql打印时间总和,但我不能帮助我。

$SQL = "SELECT duration FROM core_network ";
$result = mysql_query($SQL);


$c_count='0';
$n="0";
$duration=array();
while ($db_field = mysql_fetch_array($result)) {
$duration[$n]= $db_field['duration'];
 echo $duration[$n];
$n++;
$c_count++;
 }





the result is 18:00:0000:39:0008:00:00


than i found the code from internet


function sum_the_time($Duration) {
 $times = array('18:00:00','00:39:00','08:00:00');
 $seconds = 0;
 foreach ($times as $time)
 {
   list($hour,$minute,$second) = explode(':', $time);
   $seconds += $hour*3600;
   $seconds += $minute*60;
   $seconds += $second;
 }
 $hours = floor($seconds/3600);
 $seconds -= $hours*3600;
 $minutes  = floor($seconds/60);
 $seconds -= $minutes*60;
  // return "{$hours}:{$minutes}:{$seconds}";
 return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
  }
 echo sum_the_time($duration);

如何输入$ duration [$ n]到$ times = array('18:00:00','00:39:00','08:00:00');保证时间

1 个答案:

答案 0 :(得分:0)

将$ duration作为参数传递给函数:

function sum_the_time($times) {
 $seconds = 0;
 foreach ($times as $time)
 {
   list($hour,$minute,$second) = explode(':', $time);
   $seconds += $hour*3600;
   $seconds += $minute*60;
   $seconds += $second;
 }
 $hours = floor($seconds/3600);
 $seconds -= $hours*3600;
 $minutes  = floor($seconds/60);
 $seconds -= $minutes*60;
  // return "{$hours}:{$minutes}:{$seconds}";
 return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
  }
 echo sum_the_time($duration);