All pastes #412330 Raw Edit

Unnamed

public text v1 · immutable
#412330 ·published 2007-03-27 17:09 UTC
rendered paste body
function sec2hms ($sec, $padHours = false) {

    // holds formatted string
    $hms = "";
    
    // there are 3600 seconds in an hour, so if we
    // divide total seconds by 3600 and throw away
    // the remainder, we've got the number of hours
    $hours = intval(intval($sec) / 3600); 

    // add to $hms, with a leading 0 if asked for
    if($hours > 0) {
		$hms .= ($padHours) 
			  ? str_pad($hours, 2, "0", STR_PAD_LEFT). ' hours, '
			  : $hours. ' hours, ';
	}
     
    // dividing the total seconds by 60 will give us
    // the number of minutes, but we're interested in 
    // minutes past the hour: to get that, we need to 
    // divide by 60 again and keep the remainder
    $minutes = intval(($sec / 60) % 60); 

    // then add to $hms (with a leading 0 if needed)
    if($minutes > 0) {
		$hms .= $minutes.' minutes, ';
	}

    // seconds are simple - just divide the total
    // seconds by 60 and keep the remainder
    $seconds = intval($sec % 60); 

    // add to $hms, again with a leading 0 if needed
    $hms .= $seconds.' seconds';

    // done!
    return $hms;
    
}