从数组计算多个日期的最佳方案?-PHP
我有一系列类似这样的项目:从数组计算多个日期的最佳方案?-PHP,php,arrays,datetime,Php,Arrays,Datetime,我有一系列类似这样的项目: Array ( [0] => 0:16:0 [1] => 0:0:8 [2] => 0:5:0 ... [n] => 0:3:1 ) 可能会有更多的数组,它们是符号 小时,分钟,秒。 如何计算0+1+2+…+n要获得最终的小时、分钟和秒数?请尝试此代码 $arr = array('0:16:0', '25:12:5', '0:0:10', '0:5:0'); // converting all the
Array
(
[0] => 0:16:0
[1] => 0:0:8
[2] => 0:5:0
...
[n] => 0:3:1
)
可能会有更多的数组,它们是符号
小时,分钟,秒。
如何计算0+1+2+…+n要获得最终的小时、分钟和秒数?请尝试此代码
$arr = array('0:16:0', '25:12:5', '0:0:10', '0:5:0');
// converting all the times to seconds
$t_seconds = 0;
foreach ($arr as $v) {
sscanf($v, "%d:%d:%d", $hours, $minutes, $seconds);
$t_seconds += $hours * 3600 + $minutes * 60 + $seconds;
}
// condition if seconds calculated are negative
$sign = ($t_seconds < 0 ? '-' : '');
$t_seconds = abs($t_seconds);
// converting seconds, taking care of wrong minutes/seconds formats like 02:63:65
$hours = floor($t_seconds / 3600);
$minutes = floor(($t_seconds / 60) % 60);
$seconds = $t_seconds % 60;
// final format
$sum = $sign . sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
我通过将小时、分钟、秒作为数组从我的foreach元素中分离出来来解决这个问题。然后我调用array,使用array\u sum (感谢Rizier123为我指出了该函数)
当您现在尝试这样做时?请分享一些代码和您面临的特定问题,我们不是来为您编码的。
25:33:15
// receive values and calculate with current hours
$finalHourPause = array_sum($pauseArrayHours) . "\n";
$finalMinutePause = array_sum($pauseArrayMinutes) . "\n";
$finalSecoundsPause = array_sum($pauseArraySecounds) . "\n";