Convert seconds to hours, minutes and seconds (PHP)

Here is a small function that converts number of seconds to an array containing hours, minutes and seconds separately.

  1. <?php
  2.  
  3. /**
  4.  * Convert number of seconds into hours, minutes and seconds
  5.  * and return an array containing those values
  6.  *
  7.  * @param integer $seconds Number of seconds to parse
  8.  * @return array
  9.  */
  10. function secondsToTime($seconds)
  11. {
  12.     // extract hours
  13.     $hours = floor($seconds / (60 * 60));
  14.  
  15.     // extract minutes
  16.     $divisor_for_minutes = $seconds % (60 * 60);
  17.     $minutes = floor($divisor_for_minutes / 60);
  18.  
  19.     // extract the remaining seconds
  20.     $divisor_for_seconds = $divisor_for_minutes % 60;
  21.     $seconds = ceil($divisor_for_seconds);
  22.  
  23.     // return the final array
  24.     $obj = array(
  25.         "h" => (int) $hours,
  26.         "m" => (int) $minutes,
  27.         "s" => (int) $seconds,
  28.     );
  29.     return $obj;
  30. }

The first thing we need to do is get the number of hours. To do that we divide $seconds by 3600 (number of seconds in one hour) and use only the whole number of the result.

For example, if the supplied time was 4572 seconds then the result of the division would be 1, because 4572 / 3600 is 1.27, which means that there is only one full hour in 4572 seconds.

Next step is to find out the number of minutes in the remaining time. This is done by dividing the total number of seconds with number of seconds in $hours and using only the remainder (please see {php:operators.arithmetic} section in PHP manual for more information on the modulus operator). Then we do the same thing as we did with $hours, except with minutes - we divide the remaining number of seconds with 60 and use only the whole number.

Let use the same number of seconds for an example - 4572. The $divisor_for_minutes will be 900 because 4500 % 3600 is 972 (you can only fit 3600 inside 4572 once and after doing that the remaining number is 972).

The last step in our calculations is getting the seconds part of the time. This is pretty easy as we do exactly the same thing as we did with minutes except now, but instead we do the modulus division with 60 and not with 3600.

In our example case, $divisor_for_seconds (number of seconds in minutes and seconds) will be 972 % 60 = 12 (you can fit 60 inside 972 sixteen full times (60 * 16 = 960) and the remainder afterwards is 12). And this is our seconds part.

I think the rest is clear and doesn't need any explanation.

As always - any comments and/or suggestions will be welcome. ;)

NOTE: a JavaScript implementation is also available for this function. You can find it here: Convert seconds to hours, minutes and seconds (JavaScript).

Comments
1

$h = (int)($seconds / 3600);
$m = (int)(($seconds - $h*3600) / 60);
$s = (int)($seconds - $h*3600 - $m*60);
return (($h)?(($h<10)?("0".$h):$h):"00").":".(($m)?(($m<10)?("0".$m):$m):"00").":".(($s)?(($s<10)?("0".$s):$s):"00");
Florin, February 24th 2011, 13:38
2
for 4572 will return 01:16:12
Florin, February 24th 2011, 13:40
3
That solution works too. However, it won't make the function much smaller if you actually add line breaks and spread out the code.

Still, it's a good solution if you want to get the time as a string.
Andris, February 26th 2011, 15:48
4
basic version:
function sec_to_his ($seconds)
{
  return gmdate ('H:i:s', $seconds);
}

slightly fancier version:

function sec_to_his ($seconds)
{
  if ($seconds > 172800) // two days or more
  {
    $format = 'j \d\a\y\s H:i:s';
  }
  else if ($seconds > 86400) // more than one day, less than two
  {
    $format = 'j \d\a\y H:i:s';
  }
  else // less than a day
  {
    $format = 'H:i:s';
  }

  return gmdate ($format, $seconds);
}
zrvan, October 4th 2011, 14:34
5
hmm.. noticed that the slightly fancier version doesn't quite solve the spinning of the basic version as intended, but for anything under 24 hours the basic version will do the trick...
zrvan, October 4th 2011, 14:40
6
Couldn't leave it alone...

slightly fancier v2:
function time ($seconds)
{
  $days = floor ($seconds / 86400);
  if ($days > 1) // 2 days+, we need days to be in plural
  {
    return $days . ' days ' . gmdate ('H:i:s', $seconds);
  }
  else if ($days > 0) // 1 day+, day in singular
  {
    return $days . ' day ' . gmdate ('H:i:s', $seconds);
  }

  return gmdate ('H:i:s', $seconds);
}
zrvan, October 4th 2011, 15:30
7
Thanks buddy. this is working fine.
Thiwanka, November 3rd 2011, 5:11
8

		function sec2view($seconds)
		{
			/**
			* Convert number of seconds into years, days, hours, minutes and seconds
			* and return an string containing those values
			*
			* @param integer $seconds Number of seconds to parse
			* @return string
			*/

			$y = floor($seconds / (86400*365.25));
			$d = floor(($seconds - ($y*(86400*365.25))) / 86400);
			$h = gmdate('H', $seconds);
			$m = gmdate('i', $seconds);
			$s = gmdate('s', $seconds);

			$string = '';

			if($y > 0)
			{
			$yw = $y > 1 ? ' years ' : ' year ';
			$string .= $y . $yw;
			}

			if($d > 0)
			{
			$dw = $d > 1 ? ' days ' : ' day ';
			$string .= $d . $dw;
			}

			if($h > 0)
			{
			$hw = $h > 1 ? ' hours ' : ' hour ';
			$string .= $h . $hw;
			}

			if($m > 0)
			{
			$mw = $m > 1 ? ' minutes ' : ' minute ';
			$string .= $m . $mw;
			}

			if($s > 0)
			{
			$sw = $s > 1 ? ' seconds ' : ' second ';
			$string .= $s . $sw;
			}

		return preg_replace('/\s+/',' ',$string);
		}

		$res = sec2view(time());
		echo $res;
		
Jim Bauer, July 24th 2012, 15:58
9
Why reinvent the wheel?

gmdate ('H:i:s', $seconds);
iamDimka, August 22nd 2012, 9:51
10
This comment has been deleted
ghfgh, August 6th 2014, 14:28
Name
Email (required)
will not be published
Website
Recaptcha
you will only be required to fill it in once in this session

You can use [code][/code] tags in your comments