如何将hh:mm:ss转换为分钟

时间:2014-07-26 21:24:35

标签: php

我有一个时间列$data['Time'] (hh:mm:ss),我需要将其转换为分钟。我怎样才能做到这一点?当我写这样的时候:

$avg = ($data['Kilometers'] / $data['Time']) * 60;

我有这个错误

Warning: Division by zero in ... on line ..

7 个答案:

答案 0 :(得分:8)

$time    = explode(':', $data['Time']);
$minutes = ($time[0] * 60.0 + $time[1] * 1.0);
$avg     = $minutes > 0 ? $data['Kilometers'] / $minutes : 'inf'; // if time stored is 0, then average is infinite.

将时间戳转换为分钟的另一种方法是,

$time    = date('i', strtotime($data['Time']);

答案 1 :(得分:5)

尝试这样的事情:

function minutes($time){
$time = explode(':', $time);
return ($time[0]*60) + ($time[1]) + ($time[2]/60);
}

LIVE DEMO

答案 2 :(得分:1)

You can use following function 

<?php

function date2min ($hms) {

    $fromTime = strtotime($hms);

    $getMins = round(abs($fromTime) / 60,2);

    return $getMins;
}


$date   = date('H:i:s');  // $data['Time'] your desired time
$myResult = date2min($date);

and then use $myResult value according to your need.

?>

答案 3 :(得分:1)

如果hh:mm:ss是一个刺痛你可以试试这个经过测试并且有效:

 $hour=substr($string, 0,1);// we get the firs two values from the hh:mm:ss string
    $hour=(int)$hour;
    $hourtomin=$hour*60;// after we have the hour we multiply by 60 to get the min
    $min=substr($string, 3,4);//now we substring 3 to 4 because we want to get only  
    // the min and we dont want to get the : which is in position 2
    $min=(int)$min;

   $totalmin=$hourtomin+$min;// we just do a simple calc to calculate all the min
    echo $totalmin;

答案 4 :(得分:1)

这是一个简单的计算使用这个

    $time='01:02:10';
    $timesplit=explode(':',$time);
    $min=($timesplit[0]*60)+($timesplit[1])+($timesplit[2]>30?1:0);
    echo $min.' Min'  // 62 Min

答案 5 :(得分:0)

function hourMinute2Minutes($strHourMinute) {
    $from = date('Y-m-d 00:00:00');
    $to = date('Y-m-d '.$strHourMinute.':00');
    $diff = strtotime($to) - strtotime($from);
    $minutes = $diff / 60;
    return (int) $minutes;
}

echo hourMinute2Minutes('01:30'); // 90

答案 6 :(得分:0)

您可以使用这种非常简单的方法。

$minutes=$item['time_diff'];
$hours =   sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60));
return $hours;

输出将 09:10