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:

<br />
&quot;02:19:53&quot;<br />
&quot;02:29:47&quot;<br />
&quot;00:12:50&quot;<br />
&quot;00:08:22&quot;<br />
&quot;00:09:50&quot;<br />
&quot;02:06:19&quot;<br />
&quot;00:16:06&quot;<br />
// Total Time?<br />

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 🙂

<br />
class ReportClass {</p>
<p>	/**<br />
	 *    Convert the TimeStamp into Seconds, for then<br />
     *    we can later convert the integer create into a cumulative total.<br />
	**/<br />
	private function addDurationAsSeconds( $timeStamp ) {<br />
		$timeSections = explode( ':', $timeStamp );<br />
		$seconds =  ( $timeSections[0] * 3600 )      //Hours to Seconds<br />
                 +  ( $timeSections[1] * 60 )        //Minutes to Seconds<br />
                 +  ( $timeSections[2]  );           //Seconds to Seconds</p>
<p>		return $seconds;<br />
	}</p>
<p>   /**<br />
    *<br />
   **/<br />
   public function getTotalTimeTaken() {</p>
<p>      $getTotalTime = 0;<br />
      $reportResults = $mysqli-&gt;query(<br />
                                 &quot;SELECT<br />
                                    TIMEDIFF( TIME1, TIME2 ) AS TIME_DIFFERENCE<br />
                                  FROM<br />
                                    REPORT&quot; );</p>
<p>      while( $row =&gt; $reportResults-&gt;fetch_array()) {<br />
          $getTotalTime += $this-&gt;addDurationAsSeconds( $row[&quot;TIME_DIFFERENCE&quot;] );<br />
      }</p>
<p>      echo gmdate( $getTotalTime );<br />
   }</p>
<p>}<br />