2017-04-06 30 views
0

我在我的數據庫中有一個名爲hours_worked的列,格式爲Time00:00:00)。當我使用PHP執行時間數學計算時,它將轉換爲整數。如何在使用php執行時間數學時保持Mysql時間格式?

所以,當我打印:

<?=$deposit26['hours_worked'];?> 

輸出是30:30:15這是偉大的。

但是當我嘗試用另一個查詢執行時間數學:

<?=$total_hours_worked = $deposit26['hours_worked'] + $deposit262['hours_worked'];?> 

輸出成爲60。我想保留原來的Time格式。這怎麼可能?

回答

1

可能是因爲您的$total_hours_worked被假定爲整數。

嘗試<?=$deposit26['hours_worked'] += $deposit262['hours_worked'];?>

0

使用PHP DateInterval類。 http://www.php.net/manual/en/class.dateinterval.php

30:30:15將代表ISO 8601 PT30H30M15S

$t1 = new DateInterval('PT30H30M15S'); 

要將其他DateInterval添加到...

$t2 = new DateInterval('PT05H55M55S'); 

,我們可以通過研磨和做加法。秒,分鐘和小時...

// add seconds, carry minutes 
$t1->s += $t2->s ; 
$t1->i += (int)($t1->s/60) ; 
$t1->s = $t1->s % 60 ; 

// add minutes, carry hours 
$t1->i += $t2->i ; 
$t1->h += (int)($t1->i/60) ; 
$t1->i = $t1->i % 60 ; 

// add hours 
$t1->h += t2->h ; 

// format as string 
echo $t1->format('%h:%I:%S') ; 

或者,利用擴展DateIterval,如一下Gmail點com在PHP文件的註釋部分由glavic提供的一類http://www.php.net/manual/en/class.dateinterval.php