Search code examples
phptimemilliseconds

How to convert a time to milliseconds using PHP


I need to convert a PHP time which is into milliseconds, I have tried using the "time * 1000" but the result is not correct.

example time: 01:26.7 (1 minute, 26 seconds, 7 milliseconds)


Solution

  • Try to split the parts and convert every part to miliseconds then return the sum, something like:

    $time1   = explode(":", "01:26.7");
    $time2   = explode(".", $time1[1]);
    
    $minute  = $time1[0] * 60 * 1000;
    $second  = $time2[0] * 1000;
    $milisec = $time2[1];
    
    $result = $minute + $second + $milisec;
    
    echo $result;
    

    Live working example

    You can use DateTime class or a library for that, check:

    Convert date to milliseconds in laravel using Carbon