1

我正在尝试将 Time 格式转换00:00:00为 decimal 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() 错误的调用。我究竟做错了什么?

4

1 回答 1

0

好吧,由于某种原因,我编写的代码从未对我有用。因此,在四处寻找更具体的东西后,我偶然发现了Philip Norton 代码,它就像一个带有动态变量的魅力。你自便。

脚本

<?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 位小数

使用四舍五入只能得到 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
于 2017-04-08T23:19:47.357 回答