将时间字符串转换为十进制小时PHP [英] Convert Time String to Decimal Hours PHP

查看:198
本文介绍了将时间字符串转换为十进制小时PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <$> 

c $ c><?php

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

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

echo $ decimalHours;

?>


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;

?>

这篇关于将时间字符串转换为十进制小时PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆