How to get a total sum of Hours, Minutes & Seconds in PHP

A quick one from a small report in PHP I had to create today at work. Whereas I was producing multiple rows of Hours:minutes:Seconds for each ‘job line’ that was actioned on the companies’ internal system. As a small total, I simply wanted to produce a ‘total time taken’. The Hours:minutes:seconds was produced by MySQL’s TIMEDIFF function from the MySQL query, Example data:

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

It was through this question on StackOverflow: “Convert seconds to Hour:Minute:Second” that pointed me towards gmdate(), as date() for me was producing incorrect results. Although, keep in mind it has to be less than 24 hours. Thankfully for me, the working day for the staff is only 8-10 Hours! So it suited me just fine ðŸ™‚

```class ReportClass {

/**
*    Convert the TimeStamp into Seconds, for then
*    we can later convert the integer create into a cumulative total.
**/
private function addDurationAsSeconds( \$timeStamp ) {
\$timeSections = explode( ':', \$timeStamp );
\$seconds =  ( \$timeSections[0] * 3600 )      //Hours to Seconds
+  ( \$timeSections[1] * 60 )        //Minutes to Seconds
+  ( \$timeSections[2]  );           //Seconds to Seconds

return \$seconds;
}

/**
*
**/
public function getTotalTimeTaken() {

\$getTotalTime = 0;
\$reportResults = \$mysqli->query(
"SELECT
TIMEDIFF( TIME1, TIME2 ) AS TIME_DIFFERENCE
FROM
REPORT" );

while( \$row => \$reportResults->fetch_array()) {
\$getTotalTime += \$this->addDurationAsSeconds( \$row["TIME_DIFFERENCE"] );
}

echo gmdate( \$getTotalTime );
}

}
```