while循环中的php sum变量 我必须"总结"变量的值在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循环的所有结果。所以,我想算一下:01:00:03,01:03:04,01:10:15 = 03: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
我怎么能用PHP做到这一点? 感谢
答案 0 :(得分: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);
答案 1 :(得分: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;
答案 2 :(得分: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
答案 3 :(得分:0)
如果您的示例中的格式已修复,您可以使用DateTime-Object和Date-Interval以及此类...(有关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