Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output is in seconds. convert to hh:mm:ss format in php

Tags:

php

time

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

like image 291
Scorpion King Avatar asked Aug 20 '10 20:08

Scorpion King


People also ask

How do you convert seconds to HH mm SS?

To convert seconds to HH:MM:SS :Multiply the seconds by 1000 to get milliseconds.

How convert seconds to hours minutes and seconds in PHP?

php //PHP program to convert seconds into //hours, minutes, and seconds $seconds = 6530; $secs = $seconds % 60; $hrs = $seconds / 60; $mins = $hrs % 60; $hrs = $hrs / 60; print ("HH:MM:SS-> " . (int)$hrs .

How do I convert seconds to time?

There are 3,600 seconds in 1 hour. The easiest way to convert seconds to hours is to divide the number of seconds by 3,600.

How do you convert seconds to hours and minutes?

Converting between hours, minutes, and seconds using decimal time is relatively straightforward: time in seconds = time in minutes * 60 = time in hours * 3600. time in minutes = time in seconds / 60 = time in hours * 60. time in hours = time in minutes / 60 = time in seconds / 3600.


1 Answers

1)

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

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

prints

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

2)

echo round($time, 2);
like image 128
VolkerK Avatar answered Oct 12 '22 23:10

VolkerK