-1

我正在做一些事情,我需要以以下格式添加时间列表 mm:ss 所以 3:10 将是 3 分 10 秒。

那么,例如,您将如何在 php 中进行以下总和?

2:10 + 3:15 + 6:59 + 2:22
4

3 回答 3

3

使用以下代码:

$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); 
}

这是演示:http ://codepad.org/dJBY78wZ

于 2012-08-21T18:46:10.070 回答
1

纯娱乐 :)

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);
}

http://viper-7.com/txkHAs

于 2012-08-21T19:07:55.007 回答
1
<?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);

http://codepad.viper-7.com/BUkRRM

于 2012-08-21T19:11:17.717 回答