I have a function which converts seconds to the format of Hours:Minutes:Seconds
function sec2hms ($sec)
{
$hms = "";
$hours = intval(intval($sec) / 3600);
$minutes = intval(($sec / 60) % 60);
$seconds = intval($sec % 60);
$hms .= str_pad($hours, 2, "0", STR_PAD_LEFT). ":";
$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":";
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
return $hms;
}
How can I change this function so that all leading digits beyond the first minute position will be stripped?
For example
sec2hms(9) //returns 0:09
sec2hms(123) //returns 2:03
sec2hms(5941)//returns 1:39:01
Here’s the function I just wrote