Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP转换分钟数+;秒到秒_Php_Time - Fatal编程技术网

PHP转换分钟数+;秒到秒

PHP转换分钟数+;秒到秒,php,time,Php,Time,目前,我的时间格式如下: 0m12.345s 2m34.567s 我把它们放在变量$time中。如何在PHP中仅将变量转换为秒,例如第一个变量为12.345,第二个变量为154.567。您可以这样做 //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 remov

目前,我的时间格式如下:

0m12.345s
2m34.567s

我把它们放在变量
$time
中。如何在PHP中仅将变量转换为秒,例如第一个变量为
12.345
,第二个变量为
154.567

您可以这样做

//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];


此处演示

首先将分钟数和秒数解析为单独的变量,然后将分钟数乘以60,最后将数字相加。时间中是否包含m和s?
<?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;
    }

?>