Converting seconds to minutes and seconds

Some 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:-

  1. Make it more aware of whether any minutes exist.
  2. Enhance it so that it can also work with hours, days etc.

One Response to “Converting seconds to minutes and seconds”

  1. Danltn Says:

    A nice little function to add to my list, thanks Peter.

    Dan.

Leave a Reply