2016-08-20 149 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 –