tags:

views:

1839

answers:

10

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.

What would be the best approach?

+2  A: 

Something like this?

$vals = explode(':', $duration);

if ( $vals[0] == 0 )
   $result = $vals[1] . ' minutes, ' . $vals[2] . ' seconds';
else
   $result = $vals[0] . 'hours, ' . $vals[1] . ' minutes, ' . $vals[2] . ' seconds';
Mladen Mihajlovic
Thats what I had in mind, but I was hoping for a more elegant solution, that would perhaps involve a built in php function.
Yegor
I do not think such a function exists. Anyway, if you move this code out into a seperate function instead of using it inline, it should be just as good
Nathan Reed
Of course such a function exists!! The DateTime Object and date(strtotime()) handle this just fine. I posted an example.
enobrev
+3  A: 

try using split

list($hh,$mm,$ss)= split(':',$duration);
paan
I like that! Very nice!
Yegor
+1  A: 

One little change could be:

$vals = explode(':', $duration);

if ( $vals[0] == 0 )
   $result = "{$vals[1]} minutes, {$vals[2]} seconds";
else
   $result = "{$vals[0]} hours, {$vals[1]} minutes, {$vals[2]} seconds";
Mladen Mihajlovic
A: 

explode() is for pansies. This is a job for regular expressions!

<?php
preg_match('/^(\d\d):(\d\d):(\d\d)$/', $video_duration, $parts);
if ($parts[1] !== '00') {
    echo("{$parts[1]} hours, {$parts[2]} minutes, {$parts[3]} seconds");
}
else {
    echo("{$parts[2]} minutes, {$parts[3]} seconds");
}

Totally untested, but something like that ought to work. Note that this code assumes that the hour fragment will always be two digits (eg, a three-hour video would be 03:00:00 instead of 3:00:00).

EDIT: 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.

Garrett Albright
+1  A: 

Pretty simple:

list( $h, $m, $s) = explode(':', $hms);
echo ($h ? "$h hours, " : "").($m ? "$m minutes, " : "").(($h || $m) ? "and " : "")."$s seconds";

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.

Nathan Strong
A: 

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.

<?php
// your time
$var = "00:00:00";

if(substr($var, 0, 2) == 0){
    $myTime = substr_replace(substr_replace($var, '', 0, 3), ' Minutes, ', 2, 1);
}
elseif(substr($var, 1, 1) == 1){
$myTime = substr_replace(substr_replace($var, ' Hour, ', 2, 1), ' Minutes, ', 11, 1);   
    }
else{
$myTime = substr_replace(substr_replace($var, ' Hours, ', 2, 1), ' Minutes, ', 12, 1);
}
// work with your variable
echo  $myTime .' Seconds';

?>
Steve Obbayi
A: 

If you really want to use a built-in function, perhaps for robustness, you can try date_default_timezone_set('UTC'); $date = strtotime($hms,0); and use any of the date formatting functions (date(), strftime(), etc) to format the time in any way you wish. Or you can use the output of strptime($hms,'%T'). Either may be overkill for the simple scenario you have.

A: 

Converting 00:00:00 to hours, minutes, and seconds in PHP is really easy.

$hours = 0; $minutes = 0; $seconds = 0;

Curtis Lassam
A: 

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 :

print gmdate($seconds >= 3600 ? 'H:i:s' : 'i:s', $seconds); (for your question)

or to search on the length in a database:

SELECT * FROM videos WHERE length > 300; for example, to search for video with a length higher than 5 minutes.

+1  A: 

Why bother with regex or explodes when php handles time just fine?

$sTime   = '04:20:00';
$oTime   = new DateTime($sTime);
$aOutput = array();
if ($oTime->format('G') > 0) {
 $aOutput[] = $oTime->format('G') . ' hours';
}
$aOutput[] = $oTime->format('i') . ' minutes';
$aOutput[] = $oTime->format('s') . ' seconds';
echo implode(', ', $aOutput);

The benefit is that you can reformat the time however you like (including am/pm, adjustments for timezone, addition / subtraction, etc).

enobrev