将时间字符串转换为小数小时PHP

Jos*_*ams 5 php mysql format time

我想在PHP中将时间字符串(例如2:12:0)转换为小时格式(ex 2:12:0将是2.2小时).

Fáb*_*lva 20

使用冒号爆炸从我的头顶进行相当愚蠢的转换:

<?php 

$hms = "2:12:0";
$decimalHours = decimalHours($hms);

function decimalHours($time)
{
    $hms = explode(":", $time);
    return ($hms[0] + ($hms[1]/60) + ($hms[2]/3600));
}

echo $decimalHours;

?>
Run Code Online (Sandbox Code Playgroud)