PHP average time from an array

后端 未结 4 481
逝去的感伤
逝去的感伤 2021-01-18 18:48

How do I work out the average time from an array of times.

I have an array that looks like this :

(\'17:29:53\',\'16:00:32\')

And I

相关标签:
4条回答
  • 2021-01-18 19:23
    $times = array('17:29:53','16:00:32');
    
    $totaltime = '';
    foreach($times as $time){
            $timestamp = strtotime($time);
            $totaltime += $timestamp;
    }
    
    $average_time = ($totaltime/count($times));
    
    echo date('H:i:s',$average_time);
    
    0 讨论(0)
  • 2021-01-18 19:24
    1. Loop over all Entries and add times, converted to seconds
    2. Divide by length of Array and convert back to hh:mm:ss
    0 讨论(0)
  • 2021-01-18 19:25
    • convert both using strtotime() function
    • add the value
    • divide by 2 (or how many items there in your array)
    • convert back to normal time format
    0 讨论(0)
  • 2021-01-18 19:27
    date('H:i:s', array_sum(array_map('strtotime', $array)) / count($array))
    

    Untested solution typed on my phone, should work though.

    0 讨论(0)
提交回复
热议问题