回答
date()
和localtime()
都使用了,除非覆蓋了服務器的本地時區;您可以覆蓋使用date_default_timezone_set()
的時區。
http://www.php.net/manual/en/function.date-default-timezone-set.php
PHP的strtotime
功能將演繹時區代碼,像UTC。如果你從沒有時區代碼的數據庫/客戶端獲得日期,但知道它是UTC,那麼你可以追加它。
假設你得到時間戳代碼的日期(如 「週五2012年3月23日22時23分03秒GMT-0700(PDT)」,這是Javascript代碼""+(new Date())
給出):
$time = strtotime($dateWithTimeZone);
$dateInLocal = date("Y-m-d H:i:s", $time);
或者,如果你不這樣做,這可能是從MySQL,那麼:
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
第二個代碼片段是我需要將sqlite的datetime()函數的輸出從UTC轉換爲localtime所需的代碼片段。 – fuenfundachtzig 2014-10-06 13:10:48
謝謝,你節省了我的時間。 – Abbas 2015-12-12 10:49:35
首先,獲取日期在UTC - 你已經這樣做了所以這一步真的只是一個數據庫調用:
接下來,設置你的本地時區在PHP中:
$timezone = "America/Guayaquil";
date_default_timezone_set($timezone);
並立即在幾秒鐘內偏移:
$offset = date('Z', strtotime($utc));
print "offset: $offset \n";
最後,添加偏移到你原來的日期時間的整數時間戳:
print "LOCAL: " . date('r', strtotime($utc) + $offset) . "\n";
如果您只是想在不同的時區顯示相同的時間戳,則不需要日期算術:
$format = "M d, Y h:ia";
$timestamp = gmdate($format);
date_default_timezone_set("UTC");
$utc_datetime = date($format, $timestamp);
date_default_timezone_set("America/Guayaquil");
$local_datetime = date($format, $timestamp);
回答
轉換的UTC日期時間美洲/丹佛
// create a $dt object with the UTC timezone
$dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC'));
// change the timezone of the object without changing it's time
$dt->setTimezone(new DateTimeZone('America/Denver'));
// format the datetime
$dt->format('Y-m-d H:i:s T');
注
time()
返回unix timestamp,這是一個數字,它沒有時區。
date('Y-m-d H:i:s T')
返回當前語言環境時區中的日期。
gmdate('Y-m-d H:i:s T')
返回UTC日期
date_default_timezone_set()
改變當前區域設置時區
更改時間的時區
// create a $dt object with the America/Denver timezone
$dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('America/Denver'));
// change the timezone of the object without changing it's time
$dt->setTimezone(new DateTimeZone('UTC'));
// format the datetime
$dt->format('Y-m-d H:i:s T');
在這裏你可以看到所有可用的時區
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
這裏有所有的格式選項
http://php.net/manual/en/function.date.php
更新PHP時區數據庫(在Linux)
sudo pecl install timezonedb
這應該是上述問題的答案。 – 2016-08-10 15:11:07
這實際上回答了問題,而不是提供「如何獲得當地時間」。 – ChristoKiwi 2017-10-23 20:54:30
這裏是直接將提問者的UTC時間轉換爲本地時間即這是存儲在數據庫中的時間等,即任何時間。你只需要找到UTC時間和你感興趣的當地時間之間的時差,然後調整存儲的UTC時間就可以增加差異。
$df = "G:i:s"; // Use a simple time format to find the difference
$ts1 = strtotime(date($df)); // Timestamp of current local time
$ts2 = strtotime(gmdate($df)); // Timestamp of current UTC time
$ts3 = $ts1-$ts2; // Their difference
然後,您可以將此差異添加到存儲的UTC時間。 (在我的位置,雅典,差異恰恰是5:00:00)
鑑於當地時區,例如'America/Denver',您可以使用DateTime類將UTC時間戳轉換爲當地日期:
$timestamp = *********;
$date = new DateTime("@" . $timestamp);
$date->setTimezone(new DateTimeZone('America/Denver'));
echo $date->format('Y-m-d H:i:s');
- 1. 將本地日期時間轉換爲UTC時間在php
- 2. utc和本地日期時間轉換
- 3. UTC轉換爲當地日期時間
- 4. 將當地日期時間轉換爲UTC日期時間
- 5. 將UTC日期時間轉換爲當地日期時間
- 6. 如何將UTC日期時間轉換爲本地時間Jquery
- 7. 在Xaml中將UTC日期時間轉換爲本地時區?
- 8. 本地日期,時間和時區值轉換爲utc時區在php
- 9. 將UTC日期時間轉換爲用戶的本地日期和時間
- 10. 如何將UTC日期時間轉換爲本地日期時間?
- 11. 在UTC中將UTC時間轉換爲日期時間格式?
- 12. 轉換UTC日期時間當地日期時間
- 13. 如何UTC日期字符串轉換爲本地時間
- 14. 如何UTC日期字符串轉換爲本地時間(systemTimeZone)
- 15. 將UTC日期轉換爲Android的本地時間?
- 16. 將UTC時間戳轉換爲本地日期
- 17. 如何將GMT(UTC)datetime轉換爲本地日期時間?
- 18. 從服務器轉換UTC日期爲本地時間
- 19. UTC日期轉換爲當地時區
- 20. PHP - 將日期轉換爲UTC日期
- 21. 轉換日期時間爲UTC值MSSQL
- 22. pytz - 將日期時間轉換爲UTC
- 23. 將日期時間轉換爲SQL UTC
- 24. RestSharp將日期時間轉換爲UTC
- 25. 將UTC日期時間轉換爲日期時間偏移
- 26. 如何將UTC日期/時間轉換爲EST日期/時間
- 27. UTC datetime到設備本地日期時間轉換在android
- 28. 從UTC日期時間轉換到視圖中的本地日期時間
- 29. 本地日期和時間到UTC然後UTC到本地日期和時間
- 30. 轉換UTC GMT + 0200爲本地時間
但是,如何獲取當前UTC時間戳? – Mark 2010-09-25 01:35:09
爲什麼你需要當前的UTC時間戳來轉換保存的UTC時間戳? – Amber 2010-09-25 01:35:58
但如果你這樣做,'time()'總是返回UTC:http://us2.php.net/manual/en/function.time.php – Amber 2010-09-25 01:36:24