How do you convert 00:00:00 to hours, minutes, seconds in PHP?

I have a video duration stored in the format HH: MM: SS. I would like to show it as hours HH, minutes MM, seconds SS. It should not display hours if it is less than 1.

What would be the best approach?

+5
source share
10 answers

try using split

list($hh,$mm,$ss)= split(':',$duration);
+2
source

Something like that?

$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';
+3
source

:

$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";
+1

:

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

, , "" , , . , , "" "", , .

+1

, php ?

$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);

, , ( am/pm, , / ..).

+1

, , . 1 ... 0 0 .

<?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';

?>
0

, , ,

 date_default_timezone_set('UTC'); 
 $date = strtotime($hms,0); 

(date(), strftime() ..) . strptime($hms,'%T'). , .

0

. , . , , hh: mm: ss, :

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

:

SELECT * FROM videos WHERE length > 300; , 5 .

0

explode() . !

<?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");
}

, - . , , (, 03:00:00 3:00:00).

: - , , ; explode() , , . , , .

-1

00:00:00 , PHP .

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

-1

All Articles