Open In App

PHP Program to Convert Milliseconds to Minutes and Seconds

Last Updated : 17 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

This article will show how to convert Milliseconds to Minutes and Seconds in PHP. When working with time-related calculations, it’s common to encounter durations in milliseconds. Converting milliseconds to minutes and seconds is a useful task in various applications.

Using Division and Modulus

The basic method to convert milliseconds to minutes and seconds is by using basic arithmetic operations.

PHP




<?php
  
$milliSeconds = 150000;
  
// Convert Milli Seconds to minutes and seconds
$minutes = floor($milliSeconds / (60 * 1000));
$seconds = floor(($milliSeconds % (60 * 1000)) / 1000);
  
echo "Minutes: " . $minutes . "\n";
echo "Seconds: " . $seconds . "\n";
  
?>


Output

Minutes: 2
Seconds: 30

Using gmdate() Function

The PHP gmdate() function is used to formatting the time. It can be employed to convert milliseconds to minutes and seconds.

PHP




<?php
  
$milliSeconds = 150000;
  
// Convert Milliseconds to minutes
// and seconds 
$format = 'i:s';
  
$time = gmdate($format, $milliSeconds / 1000);
  
echo "Time: " . $time;
  
?>


Output

Time: 02:30


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads