Viral Bhoot
Viral Bhoot

Reputation: 357

To get total of time

Here, I am getting result of times. I want to sum of all the time. how this can be do?

foreach($result as $key=>$value)
                {
                    $total_markin_time[] = date('H:i:s',  strtotime($value['clock_in']));
                }

Here times are,

"02:19:53"
"02:29:47"
"00:12:50"
"00:08:22"
"00:09:50"
"02:06:19"
"00:16:06"
// Total Time?

Updated: I want result in "00:00:00" format..

This is what I have tried:

$total_markin_seconds = 0;
                foreach ( $total_markin_time as $time )
                {
                    list( $g, $i, $s ) = explode( ':', $time );
                    $total_markin_seconds += $g * 3600;
                    $total_markin_seconds += $i * 60;
                    $total_markin_seconds += $s;
                }//exit;

                $total_markin_hours    = floor( $total_markin_seconds / 3600 );
                $total_markin_seconds -= $total_markin_hours * 3600;
                $total_markin_minutes  = floor( $total_markin_seconds / 60 );
                $total_markin_seconds -= $total_markin_minutes * 60;

Upvotes: 0

Views: 52

Answers (2)

Nounours
Nounours

Reputation: 56

Try this :

$total_markin_time = 0;

foreach($result as $key=>$value)
{
    $total_markin_time += strtotime($value['clock_in']);
}

echo floor($total_markin_time/3600).':'.date('i:s', $total_markin_time);

This will sum times in seconds, and a the end convert the time to formated string.

Upvotes: 3

Ali Zia
Ali Zia

Reputation: 3875

Use this. You will get your sum of times in hh:mm:ss format.

<?php

$times = array(
    "02:19:53",
    "02:29:47",
    "00:12:50",
    "00:08:22",
    "00:09:50",
    "02:06:19",
    "00:16:06"
);

class Duration {

    public static function fromString($string) {
        $parts = explode(':', $string);
        $object = new self();
        if (count($parts) === 2) {
            $object->minutes = $parts[0];
            $object->seconds = $parts[1];
        } elseif (count($parts) === 3) {
            $object->hours = $parts[0];
            $object->minutes = $parts[1];
            $object->seconds = $parts[2];
        } else {
            // handle error
        }
        return $object;
    }

    private $hours;
    private $minutes;
    private $seconds;

    public function getHours() {
        return $this->hours;
    }

    public function getMinutes() {
        return $this->minutes;
    }

    public function getSeconds() {
        return $this->seconds;
    }

    public function add(Duration $d) {
        $this->hours += $d->hours;
        $this->minutes += $d->minutes;
        $this->seconds += $d->seconds;
        while ($this->seconds >= 60) {
            $this->seconds -= 60;
            $this->minutes++;
        }
        while ($this->minutes >= 60) {
            $this->minutes -= 60;
            $this->hours++;
        }
    }

    public function __toString() {
        return implode(':', array(sprintf("%02d", $this->hours), sprintf("%02d", $this->minutes), sprintf("%02d", $this->seconds)));
    }

}

$d1 = Duration::fromString('00:00');
foreach ($times as $time) {
    $d1->add(Duration::fromString($time));
}
echo $d1;

Upvotes: 0

Related Questions