2009-08-23 280 views

回答

7

使用strtotime()可以將當前時間(2009-09-25 15:00:00)轉換爲一個時間戳,然後添加(60 * 60 * 3 = 3小時)到時間戳。最後把它轉換回任何你想要的時間。

//Start date 
$date = '2009-09-25 15:00:00'; 
//plus time 
$plus = 60 * 60 * 3; 
//Add them 
$time = strtotime($date) + $plus; 
//Print out new time in whatever format you want 
print date("F j, Y, g:i a", $time); 
+0

建議使用此解決方案。 – dusoft 2009-08-23 16:48:06

+0

我最終使用了這個,修改日期格式爲'Y-m-d h:i:s' – 2009-08-24 23:39:30

9

簡單的方法,如果你有足夠高的版本號:

$when = new DateTime($start_time); 
$when->modify('+' . $duration); 
echo 'End time: ' . $when->format('Y-m-d h:i:s') . "\n"; 
+1

這工作對我來說,但我不得不補充。 $ duration之後的'秒',因爲我的持續時間以秒爲單位存儲。 – 2017-03-04 08:41:43

0

作爲附錄@ Xeoncross的好strtotime答案,strtotime()支持的格式,如「2009-09-253小時」,「9月25日6天」,「下週一」,「上週五」 「-15分鐘」等。

相關問題