2

Currently, I have the following time format like the following:

0m12.345s
2m34.567s

I put them in variable $time. How do I convert the variable to seconds only in PHP, like 12.345 for the first one, and 154.567 for the second one?

user2248259
  • 316
  • 1
  • 3
  • 13
  • you'd first parse the amount of minutes and amount of seconds to separate variables, then multiple minutes by 60 and finish with adding the numbers together. – eis Feb 22 '14 at 09:59
  • does the time will have m AND s in it ? – CS GO Feb 22 '14 at 10:02

2 Answers2

4

You can do it like this,

//Exploding time string on 'm' this way we have an array 
//with minutes at the 0 index and seconds at the 1 index
//substr function is used to remove the last s from your initial time string
$time_array=explode('m', substr($time, 0, -1));
//Calculating 
$time=($time_array[0]*60)+$time_array[1];
CodeBird
  • 3,883
  • 2
  • 20
  • 35
2
<?php
    $time="2m34.567s";
    $split=explode('m',$time);
//   print_r($split[0]);
    $split2=explode('s',$split[1]);

$sec= seconds_from_time($split[0])+$split2[0];
echo $sec;
function seconds_from_time($time) {

        return $time*60;
    }

?>

Demo here http://phpfiddle.org/main/code/gdf-3tj

Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30