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!
I wouldn’t bother using a regexp in this case.
Simply
explode(split) the strings with ‘:’, and perform a backward analysis – you always have seconds (and maybe milliseconds). Starting with array[lastelement] (seconds) then the previous…For instance
In PHP
In JavaScript
(just a PHP to Javascript conversion)