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

php, time

Solution

function foo($seconds) {
  $t = round($seconds);
  return sprintf('%02d:%02d:%02d', $t/3600, floor($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
echo round($time, 2);

Update Note:

function foo($seconds) {
  $t = round($seconds);
  // Deprecated: Implicit conversion from float ***.** to int loses precision
  // return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);

  // return sprintf('%02d:%02d:%02d', $t/3600, ($t%3600)/60, $t%60);
  return sprintf('%02d:%02d:%02d', $t/3600, floor($t/60)%60, $t%60);
}

Problem

My output is in the format of 290.52262423327 seconds. How can i change this to 00:04:51? 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 :)

Original source

Related problems