1

From the database I get an array containing the following values (as time):

  1. 00:00
  2. 00:02
  3. 00:01
  4. 00:02
  5. 00:02
  6. 00:02

I would like to sum this values and return the number of seconds.

I wrote a PHP script like this:

for ($i = 0; $i < count($values); $i++) {
    $time += strtotime($values[$i]);
}

echo date('s', strtotime($time);

But I get 00 as output.

What am I doing wrong?

Thank you.

Cosmin
  • 864
  • 3
  • 16
  • 34

2 Answers2

0

I think you will have to sum it separately .

Example. if you want answer in seconds and assuming HH:MM

    $seconds = 0;
   // loop through each time
    foreach ($values as $value) {
        list($hours, $minutes) = explode(':', $value);
        $seconds += $hours * 60 * 60; //convert hours to seconds and add
        $seconds += $minutes * 60; //convert minutes to seconds and add
    }

    echo $seconds; //total time in seconds

Edit: I did not add any validation eg. "25:22" . That is your job.

adi rohan
  • 796
  • 1
  • 10
  • 26
0

You have just the hour and minutes in your array, you need the seconds like 00:00:02.

Danilo
  • 41
  • 7