Converting a stopwatch time to milliseconds (regex)

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2011-01-02T03:31:46Z Indexed on 2011/01/02 3:53 UTC
Read the original article Hit count: 206

Filed under:
|
|
|

I'm trying to figure out the best way to convert a string containing a time to an integer number of milliseconds. I'm using a suboptimal way using a bunch of preg_match()'s and some array handling, but I was wondering if there was an elegant way.

Here are some example stopwatch times (some wouldn't actually be seen on a stopwatch but need to be converted anyway):

3:34:05.81

34:05

5 (just 5 seconds)

89 (89 seconds)

76:05 (76 minutes, 5 seconds)

Millseconds will not extend past 2 decimal places. You can give me an example using either PHP or Javascript regex functions.

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript