Converting seconds to minutes and seconds
Sunday, August 26th, 2007Some time ago I was in IRC and someone asked how to convert x amount of seconds into seconds and minutes. This is somewhat of an easy task and here is a simple function which can do exactly that (please note no effort was made so it would work with anything more than an hour):-
function sec_to_min($sec='')
{
if (ctype_digit((string)$sec))
{
$seconds = $sec % 60;
$minutes = ($sec-$seconds)/60;
$min_sec = $minutes.':'.str_pad($seconds, 2, '0', STR_PAD_LEFT);
return $min_sec;
}
else
{
return '0:00';
}
}
Ways in which this could be enhanced:-
- Make it more aware of whether any minutes exist.
- Enhance it so that it can also work with hours, days etc.