所以,我有以下正由API返回:我如何在PHP中減去時間?
<startTime>2011-04-12T01:28:40.000Z</startTime>
它在UTC /祖魯格式。如何從PHP中的時間戳開始獲取時間(以秒爲單位)?
感謝您的指點!
所以,我有以下正由API返回:我如何在PHP中減去時間?
<startTime>2011-04-12T01:28:40.000Z</startTime>
它在UTC /祖魯格式。如何從PHP中的時間戳開始獲取時間(以秒爲單位)?
感謝您的指點!
$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');
用'$創建現在的DateTime對象now = new DateTime(null,new DateTimeZone('UTC'));'如果服務器的當前時區不是UTC,則保證差異不會少於幾個小時。 – 2011-04-13 04:13:24
或者
$now = time();
$zulu = strtotime('2011-04-12T01:28:40.000Z');
$difference = $now - $zulul
$elapsed = time() - strtotime ($startTime);
檢查出的strtotime(http://us.php.net/manual/en/function.strtotime.php)和時間函數(http://us.php.net/manual/en/function.time.php)。基本上你會把你的時間戳轉換爲一個整數,然後從當前時間中減去它。
可能的重複:http://stackoverflow.com/questions/3642652/get-current-date-and-date-after-two-months-in-php/3642658#3642658 – 2011-04-13 03:59:14