<p>I have video durations stored in HH:MM:SS format. I'd like to display it as HH hours, MM minutes, SS seconds. It shouldn't display hours if it's less than 1.</p>

<p>What would be the best approach?</p>

[{'answer_id': 80366, 'author': 'Mladen Mihajlovic', 'author_id': 11421, 'author_profile': 'https://Stackoverflow.com/users/11421', 'pm_score': 2, 'selected': False, 'text': "<p>Something like this?</p>\n\n<pre><code>$vals = explode(':', $duration);\n\nif ( $vals[0] == 0 )\n   $result = $vals[1] . ' minutes, ' . $vals[2] . ' seconds';\nelse\n   $result = $vals[0] . 'hours, ' . $vals[1] . ' minutes, ' . $vals[2] . ' seconds';\n</code></pre>\n"}, {'answer_id': 80380, 'author': 'paan', 'author_id': 2976, 'author_profile': 'https://Stackoverflow.com/users/2976', 'pm_score': 3, 'selected': True, 'text': "<p>try using split  </p>\n\n<pre><code>list($hh,$mm,$ss)= split(':',$duration);\n</code></pre>\n"}, {'answer_id': 80389, 'author': 'Mladen Mihajlovic', 'author_id': 11421, 'author_profile': 'https://Stackoverflow.com/users/11421', 'pm_score': 1, 'selected': False, 'text': '<p>One little change could be:</p>\n\n<pre><code>$vals = explode(\':\', $duration);\n\nif ( $vals[0] == 0 )\n   $result = "{$vals[1]} minutes, {$vals[2]} seconds";\nelse\n   $result = "{$vals[0]} hours, {$vals[1]} minutes, {$vals[2]} seconds";\n</code></pre>\n'}, {'answer_id': 80395, 'author': 'Garrett Albright', 'author_id': 11023, 'author_profile': 'https://Stackoverflow.com/users/11023', 'pm_score': -1, 'selected': False, 'text': '<p>explode() is for pansies. This is a job for <a href="http://regular-expressions.info" rel="nofollow noreferrer">regular expressions</a>!</p>\n\n<pre><code>&lt;?php\npreg_match(\'/^(\\d\\d):(\\d\\d):(\\d\\d)$/\', $video_duration, $parts);\nif ($parts[1] !== \'00\') {\n    echo("{$parts[1]} hours, {$parts[2]} minutes, {$parts[3]} seconds");\n}\nelse {\n    echo("{$parts[2]} minutes, {$parts[3]} seconds");\n}\n</code></pre>\n\n<p>Totally untested, but something like that ought to work. Note that this code assumes that the hour fragment will <em>always</em> be two digits (eg, a three-hour video would be <code>03:00:00</code> instead of <code>3:00:00</code>).</p>\n\n<p><strong>EDIT:</strong> In retrospect, using regular expressions for this is probably a case of over-engineering; explode() will do the job just as well and probably even be faster in this case. But it was the first method to come to mind when I read the question.</p>\n'}, {'answer_id': 80442, 'author': 'Nathan Strong', 'author_id': 9780, 'author_profile': 'https://Stackoverflow.com/users/9780', 'pm_score': 1, 'selected': False, 'text': '<p>Pretty simple:</p>\n\n<pre><code>list( $h, $m, $s) = explode(\':\', $hms);\necho ($h ? "$h hours, " : "").($m ? "$m minutes, " : "").(($h || $m) ? "and " : "")."$s seconds";\n</code></pre>\n\n<p>This will only display the hours or minutes if there are any, and inserts an "and" before the seconds if there are hours, minutes, or both to display. If you wanted to get really fancy, you could add some code to display "hour" vs. "hours" as appropriate, ditto for minutes and seconds.</p>\n'}, {'answer_id': 80459, 'author': 'Steve Obbayi', 'author_id': 11190, 'author_profile': 'https://Stackoverflow.com/users/11190', 'pm_score': 0, 'selected': False, 'text': '<p>Heres a different way, with different functions which is more open and a more step by step for newbies. it also handles the 1 hour and many hours... you could try use the same logic to handle the 0 minutes and 0 seconds.</p>\n\n<pre><code>&lt;?php\n// your time\n$var = "00:00:00";\n\nif(substr($var, 0, 2) == 0){\n    $myTime = substr_replace(substr_replace($var, \'\', 0, 3), \' Minutes, \', 2, 1);\n}\nelseif(substr($var, 1, 1) == 1){\n$myTime = substr_replace(substr_replace($var, \' Hour, \', 2, 1), \' Minutes, \', 11, 1);   \n    }\nelse{\n$myTime = substr_replace(substr_replace($var, \' Hours, \', 2, 1), \' Minutes, \', 12, 1);\n}\n// work with your variable\necho  $myTime .\' Seconds\';\n\n?&gt;\n</code></pre>\n'}, {'answer_id': 80704, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>If you really want to use a built-in function, perhaps for robustness, you can try </p>\n\n<pre><code> date_default_timezone_set('UTC'); \n $date = strtotime($hms,0); \n</code></pre>\n\n<p>and use any of the date formatting functions (<code>date()</code>, <code>strftime()</code>, etc) to format the time in any way you wish. Or you can use the output of <code>strptime($hms,'%T')</code>. Either may be overkill for the simple scenario you have.</p>\n"}, {'answer_id': 80776, 'author': 'Curtis Lassam', 'author_id': 9337, 'author_profile': 'https://Stackoverflow.com/users/9337', 'pm_score': -1, 'selected': False, 'text': '<p>Converting 00:00:00 to hours, minutes, and seconds in PHP is really easy.</p>\n\n<p>$hours = 0; \n$minutes = 0;\n$seconds = 0; </p>\n'}, {'answer_id': 80981, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>I'll reply with a different approach of the problem. My approach is to store the lengths in seconds. Then depending the needs, it's easy to render these seconds as hh:mm:ss by using :</p>\n\n<p><code>print gmdate($seconds &gt;= 3600 ? 'H:i:s' : 'i:s', $seconds);</code> (for your question)</p>\n\n<p>or to search on the length in a database:</p>\n\n<p><code>SELECT * FROM videos WHERE length &gt; 300;</code> for example, to search for video with a length higher than 5 minutes.</p>\n"}, {'answer_id': 83497, 'author': 'enobrev', 'author_id': 14651, 'author_profile': 'https://Stackoverflow.com/users/14651', 'pm_score': 1, 'selected': False, 'text': "<p>Why bother with regex or explodes when php handles time just fine?</p>\n\n<pre><code>$sTime   = '04:20:00';\n$oTime   = new DateTime($sTime);\n$aOutput = array();\nif ($oTime-&gt;format('G') &gt; 0) {\n    $aOutput[] = $oTime-&gt;format('G') . ' hours';\n}\n$aOutput[] = $oTime-&gt;format('i') . ' minutes';\n$aOutput[] = $oTime-&gt;format('s') . ' seconds';\necho implode(', ', $aOutput);\n</code></pre>\n\n<p>The benefit is that you can reformat the time however you like (including am/pm, adjustments for timezone, addition / subtraction, etc).</p>\n"}]
['https://Stackoverflow.com/questions/80319', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']