Pages

Thursday, July 11, 2013

Convert seconds to hours, minutes and seconds - PHP


function secondsToWords($seconds)
{
    /*** return value ***/
    $ret = "";

    /*** get the hours ***/
    $hours = intval(intval($seconds) / 3600);
    if($hours > 0)   {
        if($hours <= 9) { $ret .= "0".$hours.":";}
else {       $ret .= $hours.":";}
    }
    /*** get the minutes ***/
    $minutes = bcmod((intval($seconds) / 60),60);
    if($hours > 0 || $minutes > 0)
    {
if($minutes <= 9) { $ret .= "0".$minutes.":";}
else {       $ret .= $minutes.":";}
    }
 
    /*** get the seconds ***/
    $seconds = bcmod(intval($seconds),60);
if($seconds <= 9) { $ret .= "0".$seconds;}
else {       $ret .= $seconds;}

    return $ret;
}

echo secondsToWords(3725);

No comments: