2012-04-13 39 views

回答

1

您應該使用PHP的DateTime。您可以檢查出supported formats,然後創建一個新的對象:

$date=new DateTime("2012-04-12 16:42:33"); 

此對象還支持更改時區和其他轉換。設置一個新的時區(從PHP手冊):

$date->setTimezone(new DateTimeZone('Pacific/Chatham')); 
1
strtotime('-3 hours', strtotime('2012-04-12 16:42:33')); 

http://php.net/manual/en/function.strtotime.php

更改時區,在技術上是比較麻煩一些,因爲你的時間戳格式設置的方式,我們不知道是什麼時區原來是在。所以,這個代碼就足夠了。

+0

該代碼轉換爲Unix時間戳,那麼打印回人類友好版本又該如何呢? – Francesco 2012-04-13 17:45:29

+0

@Francesco,使用PHP的'date()'格式化它,但是你想要:http://php.net/manual/en/function.date.php – Brad 2012-04-13 17:46:42

0
$hours = 3; 

$time_old = "2012-04-12 16:42:33"; 

$time_new = strtotime($time_old); 

$time_new = $time_new - (60 * $hours); 

date($time_new); 

這將允許你通過改變$hours動態改變的時間。

+0

你正在減去分鐘,而不是秒;你需要另一個'60 *'。但是,我強烈建議_不要添加手動減秒,因爲它在適用的情況下會忽略[夏令時](http://en.wikipedia.org/wiki/Daylight_saving_time)。請參閱[此評論](http://us3.php.net/manual/en/function.strtotime.php#97025)。 – Wiseguy 2012-04-13 19:06:28

+0

你的正確@Wiseguy,我的錯誤 – Dfranc3373 2012-04-13 22:26:31