0

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)

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
DCJones
  • 3,121
  • 5
  • 31
  • 53
  • If you're trying to convert that literal string `01:26.7` you'll need to parse it first, you can't just multiply it because it's not a number – ADyson Mar 16 '22 at 17:47
  • 1
    I'd expect https://www.php.net/manual/en/class.dateinterval.php can help you – ADyson Mar 16 '22 at 17:48

1 Answers1

1

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

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101