views:

87

answers:

4

I need to convert seconds to Hour:Minute:Second

for example : 685 converted to 00:11:25

can you please help me achieve this

thanks

A: 

Try this:

date("H:i:s",-57600 + 685);

Taken from
http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss

Kerry
+2  A: 

You can use the gmdate() function:

echo gmdate("H:i:s", 685);
animuson
damn you beat me to it, so you get cookies :)
Prix
Better make sure the number of seconds is below 86,400 though.
salathe
+1  A: 

One hour is 3600sec, one minute is 60sec so why not:

<?php

$init = 685;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;

echo "$hours:$minutes:$seconds";

?>

which produces:

$ php file.php
0:11:25

(I've not tested this much, so there might be errors with floor or so)

Aif
But he wants two zeros... "00:11:25" not "0:11:25"
animuson
`printf("%02d:%02d:%02d", $hours, $minutes, $seconds);`
Amber
+1  A: 

here you go

function format_time($t,$f=':') // t = seconds, f = separator 
{
  return sprintf("%02d%s%02d%s%02d", floor($t/3600), $f, ($t/60)%60, $f, $t%60);
}

echo format_time(685); // 00:11:25
nathan