【问题标题】:PHP Convert Minutes+Seconds to SecondsPHP将分钟+秒转换为秒
【发布时间】:2014-02-22 09:57:45
【问题描述】:

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

0m12.345s
2m34.567s

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

【问题讨论】:

  • 您首先将分钟数和秒数解析为分隔变量,然后将分钟数乘以 60,最后将数字相加。
  • 时间里会有m AND s吗?

标签: php time


【解决方案1】:

你可以这样,

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

【讨论】:

    【解决方案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;
        }
    
    ?>
    

    在这里演示http://phpfiddle.org/main/code/gdf-3tj

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-03
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 2015-06-02
      相关资源
      最近更新 更多