Home >> Snippet List >> Snippet

Name
Convert Seconds to Hours:Minutes:Seconds
Description
This function will convert a given number of seconds to hours, minutes, and seconds in the format HH:MM:SS, optionally padding the hours with a leading zero Example usage: $HMS = sec2hms (176, TRUE);
PHP Snippet
FUNCTION sec2hms ($sec, $padHours=FALSE) {
 
    $hours_min_secs = "";
    $hours = INTVAL(INTVAL($sec) / 3600); 
    $hours_min_secs .= ($padHours) ? STR_PAD($hours, 2, "0", STR_PAD_LEFT). ':' : $hours. ':';
    $minutes = INTVAL(($sec / 60) % 60); 
    $hours_min_secs .= STR_PAD($minutes, 2, "0", STR_PAD_LEFT). ':';
    $seconds = INTVAL($sec % 60); 
    $hours_min_secs .= STR_PAD($seconds, 2, "0", STR_PAD_LEFT);
    RETURN $hours_min_secs;
}