2016-08-20 181 views
1

PHP和变量在while循环 我要“和”变量中,而值,这里我们我的例子:PHP和时间变量while循环

while($row = mysql_fetch_array($result)){ 
    $working_hour= $row[working_hour]; 
} 

上面会输出的代码,如果我把echo $working_hour;例如: 01:00:03,01:03:04,01:10:15 我想要像sum($ working_hour)或array_sum($ working_hour)来计算while循环的所有结果。所以,我要算:1时00分03秒,1时03分04秒,01:10:15 = 3点13分22秒 我试着这样说:

$total_working_hour=’00:00:00’; 
while($row = mysql_fetch_array($result)){ 
    $working_hour= $row[working_hour]; 
    $total_working_hour+= $working_hour; 
} 

Echo $total_working_hour; 

上面的代码提供输出:

03 

我该怎么用ph​​p做到这一点? 感谢

+0

你可以与我们分享这个查询吗? –

+0

我只想提***作为一个非常重要的方面说明,你不应该使用mysql_ *函数,而应该使用PDO或MySQLi。***旧的mysql扩展已被弃用多年,并从PHP 7中删除。请参阅手册中的[选择API](http://php.net/manual/en/mysqlinfo.api.choosing.php)以获取更多关于如何更新代码的信息。 – Sherif

回答

0

我使用的答案在这里(how to sum the array time),并创建了以下功能:

function addTime($a, $b) { 
    $array = [$a, $b]; 
    $totalTimeSecs = 0; 
    foreach ($array as $time) { // Loop outer array 
     list($hours,$mins,$secs) = explode(':',$time); // Split into H:m:s 
     $totalTimeSecs += (int) ltrim($secs,'0'); // Add seconds to total 
     $totalTimeSecs += ((int) ltrim($mins,'0')) * 60; // Add minutes to total 
     $totalTimeSecs += ((int) ltrim($hours,'0')) * 3600; // Add hours to total 
    } 

    $hours = str_pad(floor($totalTimeSecs/3600),2,'0',STR_PAD_LEFT); 
    $mins = str_pad(floor(($totalTimeSecs % 3600)/60),2,'0',STR_PAD_LEFT); 
    $secs = str_pad($totalTimeSecs % 60,2,'0',STR_PAD_LEFT); 
    return "$hours:$mins:$secs"; 
    } 

所以,你可以用这个和替换

$total_working_hour+= $working_hour; 

$total_working_hour = addTime($total_working_hour, $working_hour); 
+0

非常感谢,Webinan –

+0

@ARBhuiyan不客气,考虑upvote /接受如果答案是有用的。 – Webinan

0
$hours=0;$min=0;$sec=0; 
    while($row = mysql_fetch_array($result)){ 
     $working_hour= $row[working_hour]; 
     $arr=explode(':',$working_hour); 
     $hours=$hours+$arr[0]; 
     $min=$min+$arr[1]; 
     if($min>60){$hours++;$min=$min-60;} 
     $sec=$sec+$arr[2]; 
     if($sec>60){$min++;$sec=$sec-60;} 
    } 
    echo 'Total working hours='.$hours.':'.$min.':'.$sec; 
+0

非常感谢,Satty –

+0

你知道你可以upvote;) – Satty

0

$row["working_hour"]的值显然是一个字符串。所以说像"01:00:03" + "01:03:04"这样的东西显然是没有意义的。 PHP假定你的意思是先把字符串转换为整数,然后将它们加在一起。这样的结果并不是你实际上所追求的。

相反,您希望将像"01:00:03"这样的字符串转换为标准化的整数值,如秒数,可以将它们相加,然后再转换回字符串值。

因此,要获得字符串的标准化值作为你需要这样的功能秒整...

function convertStringToNumSeconds($string) { 
    list($hours, $minutes, $seconds) = explode(":", $string, 3); 
    $totalSeconds = 0; 
    $totalSeconds += $hours * 3600; // There are 3600 seconds in an hour 
    $totalSeconds += $minutes * 60; // There are 60 seconds in a minute 
    $totalSeconds += $seconds; // There is 1 second in a second 
    return $totalSeconds; 
} 

然后到秒转换回格式化字符串,你可以做相反的事情。 ..

function secondsToString($seconds) { 
    $hours = (int) floor($seconds/3600); 
    $seconds -= $hours * 3600; 
    $minutes = (int) floor($seconds/60); 
    $seconds -= $minutes * 60; 
    return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds); 
} 

现在在你的循环,你实际上可以做这样的事情...

$totalWork = 0; 
while($row = mysql_fetch_array($result)){ 
    $totalWork += convertStringToNumSeconds($row["working_hour"]); 
} 
echo secondsToString($totalWork); // You should get your expected result 
+1

我只是想提一下***作为一个非常重要的一面,你不应该使用mysql_ *函数,而应该使用PDO或MySQLi。旧的mysql扩展已被弃用多年,并从PHP 7中删除。请参阅手册中的[选择API](http://php.net/manual/en/mysqlinfo.api.choosing.php)以获取更多信息。如何更新您的代码。 – Sherif

+0

非常感谢Sherif –

0

如果在您的示例格式是固定,你可以用日期时间,对象和DateInterval工作,以及这样的...(更多信息DateInterval,看看在PHP-的Docu)

$dt = new \DateTime('00:00:00'); 
    foreach ($dbRows as $row) { 

     $time = $row['working_hour']; 
     $timeSplit = explode(':', $time); 
     $interval = new \DateInterval(
      'P0Y0DT'.$timeSplit[0].'H'. 
      $timeSplit[1].'M'.$timeSplit[2].'S' 
     ); 
     $dt->add($interval); 
    } 

    echo $dt->format('H:i:s'); // Output: 03:13:22 
+0

非常感谢jiGL –