我正在做一些事情,我需要以以下格式添加时间列表 mm:ss 所以 3:10 将是 3 分 10 秒。
那么,例如,您将如何在 php 中进行以下总和?
2:10 + 3:15 + 6:59 + 2:22
使用以下代码:
$tarr = array('2:10', '3:15','6:59','2:22');
echo sum_the_time($tarr);
function sum_the_time($times) {
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
// $seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
// return "{$hours}:{$minutes}:{$seconds}";
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
纯娱乐 :)
function sumTime($times) {
$min = $sec = 0;
foreach($times as $time) {
list($cmin, $csec) = explode(':', $time);
$min += $cmin;
$sec += $csec;
}
return sprintf('%02d:%02d', $min + floor($sec/60), $sec % 60);
}
<?php
function s($str) {
list($minutes,$seconds) = explode(':',$str);
return $minutes * 60 + $seconds;
}
function ms($seconds) {
return sprintf('%02d:%02d', $seconds/60, $seconds%60);
}
$seconds = s('2:10') + s('3:15') + s('6:59') + s('2:22');
echo ms($seconds);