Search code examples
phpfloor

Always display time as XX:XX using PHP


All, I have the following code to figure out a time based on milliseconds that were provided:

$ms = $value['trackTimeMillis'];
$track_time = floor($ms/60000).':'.floor(($ms%60000)/1000);

The issue is that sometimes this doesn't work that well. For example, if I have the milliseconds as 246995 this will output 4:6.

Is there a way to always make it so that it converts this correct and if it does round to an even number to add a zero at the end of it? So something like 2:3 would read 2:30?

Thanks!


Solution

  • Yes:

    sprintf("%d:%02d", floor($ms / 60000), floor($ms % 60000) / 1000);