将时间字符串转换为十进制小时 PHP

2021-12-23 00:00:00 time format php mysql

我想在 PHP 中将时间字符串(例如 2:12:0)转换为以小时为单位的十进制格式(例如 2:12:0 将是 2.2 小时).

I would like to convert time strings (Such as 2:12:0) to decimal format in hours (ex 2:12:0 would be 2.2 hours) in PHP.

推荐答案

一个相当愚蠢的转换,使用冒号爆炸:

A fairly dumb conversion from the top of my head, using explode by colon:

<?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;

?>

相关文章