How to sum N number of time (HH:MM Format)?

后端 未结 5 1306
独厮守ぢ
独厮守ぢ 2020-11-27 21:43

I am using the following sample code to calculate sum of two different time values. Now I want to get the sum of N number of time values.

// numbers for test         


        
5条回答
  •  旧时难觅i
    2020-11-27 22:17

    Here is an function that will sum all your time values in format HH:MM:

    function sum_time() {
        $i = 0;
        foreach (func_get_args() as $time) {
            sscanf($time, '%d:%d', $hour, $min);
            $i += $hour * 60 + $min;
        }
        if ($h = floor($i / 60)) {
            $i %= 60;
        }
        return sprintf('%02d:%02d', $h, $i);
    }
    
    // use example
    echo sum_time('01:05', '00:02', '05:59'); # 07:06
    

    demo

提交回复
热议问题