将MysQl时间转换为十进制格式

时间:2017-04-08 00:26:17

标签: php undefined-function

我试图将时间格式00:00:00转换为小数0.00

我试过了:

<?php 
   $hourswork_wk1 = $deposit26['hours_worked'];
   $decimalHourswk1 = decimalHours($hourswork_wk1);
   decimalHours();

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


;?>

修改

<?php 
   $hourswork_wk1 = $deposit26['hours_worked'];
   $decimalHourswk1 = decimalHours($hourswork_wk1);

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

    }
?>

我调用了未定义的函数decimalHours()错误。我做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧,出于某种原因,我写的代码从来没有为我工作过。因此,在寻找更有形的东西之后,我偶然发现了Philip Norton code,它就像一个带有动态变量的魅力。适合自己。

<强>脚本

<?php
//$week1hours = $deposit26['hours_worked'];
$week1hours = "14:33:38";
function time_to_decimal($time) {
    $timeArr = explode(':', $time);
    $decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
    return $decTime;
}
echo time_to_decimal($week1hours);
?>

<强>结果

14.560555555556

奖励:限制为2位小数

使用round只能获得2位小数

<?php
//$week1hours = $deposit26['hours_worked'];
$week1hours = "14:33:38";
function time_to_decimal($time) {
    $timeArr = explode(':', $time);
    $decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
    return $decTime;
}
$totalhours= time_to_decimal($week1hours);
echo round($totalhours, 2);
?>

<强>结果

14.56