2017-04-08 159 views
1

我試圖將時間格式00:00:00轉換爲十進制0.00將MysQl時間轉換爲十進制格式

我嘗試這樣做:

<?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()的調用錯誤。我究竟做錯了什麼?

+0

把'echo'後'return'是沒有意義的。 'return'語句結束函數。 – Barmar

+0

您在'decimalHours()'後面缺少';'' – Barmar

+0

爲什麼您有'decimalHours()'這一行?它什麼都不做,而且它是無效的語法。 – Barmar

回答

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位小數

使用輪只拿到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 
+0

除了函數的名稱之外,有什麼區別? – Barmar

+0

@Barmar:我很困惑,我不知道。我希望我能告訴。 –

+0

也許Netbeans在定義它之前不喜歡你調用一個函數? PHP本身對此沒有任何問題。 – Barmar

相關問題