24 October 2023

Convert a time in the format H:i:s (hours:minutes:seconds) to milliseconds in PHP. In this code, we first split the time string into hours, minutes, and seconds using the explode function. Then, we calculate the total time in seconds by converting hours and minutes to seconds and adding the seconds. Finally, we multiply the total seconds by 1000 to get the time in milliseconds. Since we don't have any milliseconds in the first place, they will always be zeros.

Source code viewer
  1. $time = '13:37:00';
  2. list($hours, $minutes, $seconds) = explode(':', $time);
  3. $milliseconds = (($hours * 3600) + ($minutes * 60) + $seconds) * 1000;
  4.  
  5. echo $milliseconds;
  6. // Output: 49020000
Programming Language: PHP