tags:

views:

46

answers:

6
  1. My output is in the format of 290.52262423327 seconds. How can i change this to 00:04:51?

  2. The same output i want to show in seconds and in HH:MM:SS format, so if it is seconds, i want to show only 290.52 seconds.(only two integers after decimal point)? how can i do this?

I am working in php and the output is present in $time variable. want to change this $time into $newtime with HH:MM:SS and $newsec as 290.52.

Thanks :)

A: 
echo date('H:i:s',$time);

echo number_format($time,2);
Mark Baker
Won't work for time values > 12 hours, though
Pekka
Won't work for times > 24 hours
Mark Baker
A: 

Numero uno... http://www.ckorp.net/sec2time.php (use this function)

Numero duo... echo round(290.52262423327,2);

Webarto
+1  A: 

Try this:

$time = 290.52262423327;
echo date("h:i:s", mktime(0,0, round($time) % (24*3600)));
MartyIX
A: 
$iSeconds = 290.52262423327;
print date('H:i:s', mktime(0, 0, $iSeconds));
Helgi Hrafn Gunnarsson
Thanks, this worked.
Scorpion King
Won't work for values > 24 hours, though
Pekka
yes it did not work... Thanks for the tip-off pekka
Scorpion King
A: 

1)

$newtime = sprintf( "%02d:%02d:%02d", $time / 3600, $time / 60 % 60, $time % 60 );

2)

$newsec = sprintf( "%.2f", $time );
Rob K
A: 

1)

echo foo('290.52262423327'), "\n";
echo foo('9290.52262423327'), "\n";
echo foo(86400+120+6), "\n";

function foo($seconds) {
  $t = round($seconds);
  return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
}

prints

00:04:51
02:34:51
24:02:06

2)

echo round($time, 2);
VolkerK
Thanks Volker :)
Scorpion King