douhuan6157 2013-12-11 13:05
浏览 250
已采纳

计算php中经过的时间 - 超过24小时

What I am trying to do is add up hours/mins/secs that span over 24 hours, for example:

12:39:25
08:22:10
11:08:50
07:33:05

What I would like it to return is "39:43:30" rather than a date from 1970. Below is the code I'm currently using (note - its from within a class, not just a function).

private function add_time($time1, $time2)
{
$first_exploded = explode(":", $time1);
$second_exploded = explode(":", $time2);
$first_stamp = mktime($first_exploded[0],$first_exploded[1],$first_exploded[2],1,1,1970);
$second_stamp = mktime($second_exploded[0],$second_exploded[1],$second_exploded[2],1,1,1970);
$time_added = $first_stamp + $second_stamp;
$sum_time = date("H:i:s",$time_added);
return $sum_time;
}

Any advise would be much appreciated.

  • 写回答

2条回答 默认 最新

  • dsgwoh7038 2013-12-11 13:24
    关注

    Here's a nice little function that will add up any number of times passed in an array:-

    function addTimes(Array $times)
    {
        $total = 0;
        foreach($times as $time){
            list($hours, $minutes, $seconds) = explode(':', $time);
            $hour = (int)$hours + ((int)$minutes/60) + ((int)$seconds/3600);
            $total += $hour;
        }
        $h = floor($total);
        $total -= $h;
        $m = floor($total * 60);
        $total -= $m/60;
        $s = floor($total * 3600);
        return "$h:$m:$s";
    }
    

    Use it like this:-

    $times = array('12:39:25', '08:22:10', '11:08:50', '07:33:05',);
    var_dump(addTimes($times));
    

    Output:-

    string '39:43:30' (length=8)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效