Spencer Mark
Spencer Mark

Reputation: 5311

PHP : Add durations

I'm trying to figure out how to add an array of durations (video length) together to get the final length of the whole series.

The values will be in the H:i:s format:

$durations = array(
   '00:05:34',
   '00:03:26',
   '00:06:13',
   '00:05:15'
);

Is there any easy way of getting the total time for the values of this sort of array?

Upvotes: 1

Views: 418

Answers (4)

sugunan
sugunan

Reputation: 4456

Here I have developed a duration calculation function. Just copy and past it.

<?php
function duration_cal($durations)
{
   $sec = 0;
   foreach($durations as $du)
   {
        $timarr = explode(":",$du);
        $hh = $timarr[0];
        $mm = $timarr[1];
        $ss = $timarr[2];

        $sec = $sec + ($hh*60*60) + ($mm * 60) + $ss;
   }

   $hh = floor($sec/3600);
   $mm = floor(($sec%3600) / 60);
   $ss = (($sec%3600) % 60);

   $hh = str_pad($hh, 2, '0', STR_PAD_LEFT);
   $mm = str_pad($mm, 2, '0', STR_PAD_LEFT);
   $ss = str_pad($ss, 2, '0', STR_PAD_LEFT);

   return $hh.":".$mm.":".$ss;
}


$durations = array(
   '00:05:34',
   '00:03:26',
   '00:06:13',
   '00:05:15'
);

echo duration_cal($durations);
?>

Working script in this url: http://sugunan.net/demo/duration.php

Upvotes: 1

Yuriy Perevoznikov
Yuriy Perevoznikov

Reputation: 81

I believe that it is separated task from the logic you trying to implement and wrapped this as helper (or converter) class, the code is self explained:

class Helper_TimeAggregator {

    private $_durations = null;
    private $_seconds = null;

    /**
     * Constructor with init & constructor by default
     */
    public function __construct($durations = null) {
        if (isset($durations)) {
            $this->setDurations($durations);
        }
    }

    /**
     *  Setter for init
     */
    public function setDurations($newDurations) {
        $this->_durations = $newDurations;
        $this->_seconds = null;
    }

    /**
     *  Returns total seconds
     */
    public function getTotalSeconds() {
        if (!isset($this->_seconds)) {
            if (!is_array($this->_durations)) {
                return '00:00:00';
            }
            $seconds = 0;
            foreach ($this->_durations as $duration) {
                $parts = explode(':', $duration);
                if (3 === count($parts)) {
                    $seconds += $parts[0] * 3600;
                    $seconds += $parts[1] * 60;
                    $seconds += $parts[2];
                }
            }
            $this->_seconds = $seconds;
        }
        return $this->_seconds;
    }

    /**
     *  Converts to data
     */
    public function getTotalTime() {
        return date("H:i:s", $this->getTotalSeconds());
    }

    /**
     *  Using default method by default
     */
    public function __toString() {
        return $this->getTotalTime();
    }
}

Example of usage:

$durations = array(
   '00:05:34',
   '00:03:26',
   '00:06:13',
   '00:05:15'
);
$t = new Helper_TimeAggregator($durations);
echo $t;

All improvements are welcome!

Upvotes: 0

Francesco de Guytenaere
Francesco de Guytenaere

Reputation: 4833

For quick and dirty implementation;

function AddTime(Array $durations) 
{
    $total_time    = 0;
    foreach($durations as $duration) {  
        sscanf($duration, "%d:%d:%d", $hours, $minutes, $seconds);
        $total_time += isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
    }    
    return sprintf('%02d:%02d:%02d', ($total_time/3600),($total_time/60%60), $total_time%60);;
 }

$durations     = array(
   '00:05:34',
   '00:03:26',
   '00:06:13',
   '00:05:15'
);

echo AddTime($durations);

Upvotes: 6

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Found and taken/borrowed from source:
http://www.daveismyname.com/adding-multiple-times-together-in-an-array-with-php-bp

<?php 

class times_counter {

    private $hou = 0;
    private $min = 0;
    private $sec = 0;
    private $totaltime = '00:00:00';

    public function __construct($times){

        if(is_array($times)){

            $length = sizeof($times);

            for($x=0; $x <= $length; $x++){
                    $split = explode(":", @$times[$x]); 
                    $this->hou += @$split[0];
                    $this->min += @$split[1];
                    $this->sec += @$split[2];
            }

            $seconds = $this->sec % 60;
            $minutes = $this->sec / 60;
            $minutes = (integer)$minutes;
            $minutes += $this->min;
            $hours = $minutes / 60;
            $minutes = $minutes % 60;
            $hours = (integer)$hours;
            $hours += $this->hou % 24;
            $this->totaltime = $hours.":".$minutes.":".$seconds;
        }
    }

    public function get_total_time(){
        return $this->totaltime;
    }

}

// Your array
$times = array(
   '00:05:34',
   '00:03:26',
   '00:06:13',
   '00:05:15'
);

$counter = new times_counter($times);
echo $counter->get_total_time();

//outputs:
// 0:20:28

Upvotes: 0

Related Questions