Web Analytics Made Easy -
StatCounter Making a ms time display as min:sec.ms - CodingForum

Announcement

Collapse
No announcement yet.

Making a ms time display as min:sec.ms

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Making a ms time display as min:sec.ms

    Hi,

    I'm looking to take a time value which is stored in milliseconds and convert it into a readable format of 0:00.000

    I got most of the way using:

    PHP Code:
    //$totaltime = 62345

    $time_ms substr($totaltime, -3); // 345 milliseconds
    $time_sec = ($totaltime $time_ms) / 1000// 62 seconds

    while ($time_sec 60
        {
        
    $time_sec $time_sec 60;
        
    $time_min++;
        }

    echo 
    $time_min.":".$time_sec."."$time_ms// 1:2:345 
    However occasionally i'd obviously get 1:2:345
    when i'd required it to show 1:02:345

    Is there a better way of doing this rather than using 'if < 10' to shove an extra 0 in there?

    I had thought 'number_format' would do the trick, but that doesnt seem to cater for this, any ideas what would?

    Thanks

  • #2
    str_pad($time_sec, 2, '0', STR_PAD_LEFT);
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

    Comment

    Working...
    X