我有一个视频片段持续时间列表,我需要加起来才能获得总持续时间.
这个系列是这样的:
...等等
我需要加上分钟和秒来获得总视频时长.
这是我接受的答案的修改功能:
function getTotalDuration ($durations) {
$total = 0;
foreach ($durations as $duration) {
$duration = explode(':',$duration);
$total += $duration[0] * 60;
$total += $duration[1];
}
$mins = floor($total / 60);
$secs = str_pad ( $total % 60, '2', '0', STR_PAD_LEFT);
return $mins.':'.$secs;
}
Run Code Online (Sandbox Code Playgroud)
只是确保输出看起来正确.
给这个代码一个镜头:
function getTotalDuration ($durations) {
$total = 0;
foreach ($durations as $duration) {
$duration = explode(':',$duration);
$total += $duration[0] * 60;
$total += $duration[1];
}
$mins = $total / 60;
$secs = $total % 60;
return $mins.':'.$secs;
}
Run Code Online (Sandbox Code Playgroud)