2015-09-29 39 views
-6

我想通過一個小時變量創建一個時間戳。創建一個小時前的時間戳

例如: 當前時間戳是:

2015-09-29 14:00:00 

我的變量是:$hour = 2

新的時間戳,我想:

2015-09-29 16:00:00 
+2

'的strtotime(-2小時)下面連結'你所述時戳,而不是日期時間。 – Andrew

+2

檢查此鏈接:http://stackoverflow.com/questions/7811609/php-add-two-hours-to-date-variable –

回答

2

只需使用PHP的strtotime功能。

例如:

$twoHoursAgo = Date("Y-m-d H:i:s", strtotime("-2 hours")); 

更多參考Get the timestamp of exactly one week ago in PHP?

+0

它什麼也沒有返回! '$ twoHoursAgo = new Date(「Y-m-d H:i:s」,strtotime(「 - 2 hours」)); echo $ twoHoursAgo;' –

+0

@ S.M_Emamian因爲您使用了'= new Date(...'而不是'= date(...' –