PDA

View Full Version : Miliseconds 2 String


Dygear
26th February 2008, 10:34
Split 1 Split 2 Split 3
23100 51710 71480
0:23.10 0:51.71 1:11.48
------------------------

<?php
function convertMS2String($time) {
$secs = $time / 1000;
while ($secs > 60) {
$mins++;
$secs -= 60;
}
return sprintf('%d:%2.2f', $mins, $secs);
}
?>

Test 1:
23100 51710 71480
<?php echo convertMS2String(23100); ?> <?php echo convertMS2String(51710); ?> <?php echo convertMS2String(71480); ?>

Works, but I still fell dirty about it. Seems like there should be a better way.

AndroidXP
26th February 2008, 10:43
$mins = floor($time / 60000);
$secs = ($time % 60000) / 1000;

Dygear
26th February 2008, 11:13
Indeed, you might of become my favorite person.

function convert_ms2string($time) {
return sprintf('%d:%2.2f', floor($time / 60000), (($time % 60000) / 1000));
}

One line functions are the best! So what's your real name AndroidXP? I'd like to put you into the credits for LFSWorldSDK.