PHP Program to Convert Milliseconds to Minutes and Seconds
Last Updated :
17 Jan, 2024
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;
$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;
$format = 'i:s' ;
$time = gmdate ( $format , $milliSeconds / 1000);
echo "Time: " . $time ;
?>
|
Share your thoughts in the comments
Please Login to comment...