2011-04-13 80 views
2

所以,我有以下正由API返回:我如何在PHP中減去時間?

<startTime>2011-04-12T01:28:40.000Z</startTime> 

它在UTC /祖魯格式。如何從PHP中的時間戳開始獲取時間(以秒爲單位)?

感謝您的指點!

+0

可能的重複:http://stackoverflow.com/questions/3642652/get-current-date-and-date-after-two-months-in-php/3642658#3642658 – 2011-04-13 03:59:14

回答

7
$now = new DateTime; 

$zulu = new DateTime('2011-04-12T01:28:40.000Z'); 

$difference = $now->diff($zulu); 

diff()在> = PHP 5.3中受支持。您可以使用time()strtotime()

$difference = time() - strtotime('2011-04-12T01:28:40.000Z'); 
+0

用'$創建現在的DateTime對象now = new DateTime(null,new DateTimeZone('UTC'));'如果服務器的當前時區不是UTC,則保證差異不會少於幾個小時。 – 2011-04-13 04:13:24

1

或者

$now = time(); 

$zulu = strtotime('2011-04-12T01:28:40.000Z'); 

$difference = $now - $zulul 
0
$elapsed = time() - strtotime ($startTime);