1

您好,我要从我的 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'); 而是多次

4

1 回答 1

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);
于 2012-10-22T03:42:50.953 回答