dopii22884 2018-12-03 10:50
浏览 10

时间数组的总和php - 结果应该是H:m:我没有DAY

I have an array with time values

 print_r($timeVal);

output:

 Array ( 
[2018-12-01] => 23:58:0 
[2018-12-02] => 23:58:0
[2018-12-03] => 23:58:0     
[2018-12-05] => 2:0:0 
)

so i want to calculate all those and get total time: correct result should be

73:54 and not 3 days 1 hours 54 minutes 0 seconds

post is not duplicate coz i viewed other posts and they show Day hour minute not just hour min

example like this :

 $sum = strtotime('00:00:00');
 $sum2=0;  
 foreach ($TimeVal as $v){

        $sum1=strtotime($v)-$sum;

        $sum2 = $sum2+$sum1;
    }

    $sum3=$sum+$sum2;
date("H:i:s",$sum3)  //this show 1:54:00
  • 写回答

1条回答 默认 最新

  • duanlinjian5819 2018-12-03 12:14
    关注

    Since you do not want to display your time in a 24-hour format, date() won't give you what you are looking for.

    I copied your code and modified it slightly, so it does the job with the example you gave:

    $sum = strtotime('00:00:00');
    $sum1 = 0;
    foreach ($TimeVal as $v){
        $sum1 += strtotime($v) - $sum;
    }
    $hours = $sum1 / 3600;
    $minutes = ($hours - floor($hours)) * 60;
    
    echo floor($hours) . ':' . round($minutes);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线