在 PHP 中将 UTC 日期转换为本地时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3792066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert UTC dates to local time in PHP
提问by Mark
I'm storing the UTC dates into the DB using:
我使用以下方法将 UTC 日期存储到数据库中:
$utc = gmdate("M d Y h:i:s A");
and then I want to convert the saved UTC date to the client's local time.
然后我想将保存的 UTC 日期转换为客户端的本地时间。
How can I do that?
我怎样才能做到这一点?
Thanks
谢谢
采纳答案by Amber
date()
and localtime()
both use the local timezone for the server unless overridden; you can override the timezone used with date_default_timezone_set()
.
date()
并且localtime()
都使用服务器除非被覆盖本地时区; 您可以覆盖使用的时区date_default_timezone_set()
。
http://www.php.net/manual/en/function.date-default-timezone-set.php
http://www.php.net/manual/en/function.date-default-timezone-set.php
http://us3.php.net/manual/en/function.date.php
http://us3.php.net/manual/en/function.date.php
回答by webjprgm
PHP's strtotime
function will interpret timezone codes, like UTC. If you get the date from the database/client without the timezone code, but know it's UTC, then you can append it.
PHP 的strtotime
函数将解释时区代码,例如 UTC。如果您从没有时区代码的数据库/客户端获取日期,但知道它是 UTC,那么您可以附加它。
Assuming you get the date with timestamp code (like "Fri Mar 23 2012 22:23:03 GMT-0700 (PDT)", which is what Javascript code ""+(new Date())
gives):
假设您获得带有时间戳代码的日期(例如“2012 年 3 月 23 日星期五 22:23:03 GMT-0700 (PDT)”,这是 Javascript 代码""+(new Date())
给出的内容):
$time = strtotime($dateWithTimeZone);
$dateInLocal = date("Y-m-d H:i:s", $time);
Or if you don't, which is likely from MySQL, then:
或者,如果您不这样做,这可能来自 MySQL,那么:
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
回答by Timo Huovinen
Answer
回答
Convert the UTC datetime to America/Denver
将 UTC 日期时间转换为 America/Denver
// 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');
Notes
笔记
time()
returns the unix timestamp, which is a number, it has no timezone.
time()
返回unix timestamp,它是一个数字,它没有时区。
date('Y-m-d H:i:s T')
returns the date in the current locale timezone.
date('Y-m-d H:i:s T')
返回当前语言环境时区的日期。
gmdate('Y-m-d H:i:s T')
returns the date in UTC
gmdate('Y-m-d H:i:s T')
返回 UTC 日期
date_default_timezone_set()
changes the current locale timezone
date_default_timezone_set()
更改当前语言环境时区
to change a time in a timezone
更改时区中的时间
// 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');
here you can see all the available timezones
在这里你可以看到所有可用的时区
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
here are all the formatting options
这是所有格式选项
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.date.php
Update PHP timezone DB (in linux)
更新 PHP 时区数据库(在 linux 中)
sudo pecl install timezonedb
回答by fijiaaron
Date arithmetic is not needed if you just want to display the same timestamp in different timezones:
如果您只想在不同的时区显示相同的时间戳,则不需要日期算法:
$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);
回答by fijiaaron
First, get the date in UTC -- you've already done that so this step would really just be a database call:
首先,获取 UTC 日期——您已经这样做了,所以这一步实际上只是一个数据库调用:
$timezone = "UTC";
date_default_timezone_set($timezone);
$utc = gmdate("M d Y h:i:s A");
print "UTC: " . date('r', strtotime($utc)) . "\n";
Next, set your local time zone in PHP:
接下来,在 PHP 中设置您的本地时区:
$timezone = "America/Guayaquil";
date_default_timezone_set($timezone);
And now get the offset in seconds:
现在以秒为单位获取偏移量:
$offset = date('Z', strtotime($utc));
print "offset: $offset \n";
Finally, add the offset to the integer timestamp of your original datetime:
最后,将偏移量添加到原始日期时间的整数时间戳:
print "LOCAL: " . date('r', strtotime($utc) + $offset) . "\n";
回答by wdog
I store date in the DB in UTC format but then I show them to the final user in their local timezone
我以 UTC 格式将日期存储在数据库中,然后我将它们显示给本地时区的最终用户
// retrieve
$d = (new \DateTime($val . ' UTC'))->format('U');
return date("Y-m-d H:i:s", $d);
回答by Saurabh-Magento2-Developer
Here I am sharing the script, convert UTC timestamp to Indian timestamp:-
我在这里分享脚本,将UTC时间戳转换为印度时间戳:-
// create a $utc object with the UTC timezone
$IST = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC'));
// change the timezone of the object without changing it's time
$IST->setTimezone(new DateTimeZone('Asia/Kolkata'));
// format the datetime
echo $IST->format('Y-m-d H:i:s T');
回答by Apostolos
Here is a straight way to convert the UTC time of the questioner to local time. This is for a stored time in a database etc., i.e. anytime. You just have to find the time difference between UTC time and the local time you are interested in and then ajust the stored UTC time adding to it the difference.
这是将提问者的 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
You can then add this difference to the stored UTC time. (In my place, Athens, the difference is exactly 5:00:00)
然后,您可以将此差异添加到存储的 UTC 时间。(在我的地方,雅典,时差正好是 5:00:00)
Example:
例子:
$time = time() // Or any other timestamp
$time += $ts3 // Add the difference
$dateInLocal = date("Y-m-d H:i:s", $time);
回答by Henry
Given a local timezone, such as 'America/Denver', you can use DateTime class to convert UTC timestamp to the local date:
给定本地时区,例如“美国/丹佛”,您可以使用 DateTime 类将 UTC 时间戳转换为本地日期:
$timestamp = *********;
$date = new DateTime("@" . $timestamp);
$date->setTimezone(new DateTimeZone('America/Denver'));
echo $date->format('Y-m-d H:i:s');
回答by Senthur Pandi S
$UTC_Time = "2018-07-06 06:06:16";
echo "UTC Time ".$UTC_Time;
$Indian_Time = TimeConverion($UTC_Time);
echo "<br> Indian_Time ".$Indian_Time;
function TimeConverion($UTC_Time) {
date_default_timezone_set('Europe/London');
$sTime = date("Y-m-d h:i:sa");
$ts3 = strtotime(date("G:i:s"))-strtotime($sTime);
$utc = explode(" ",$UTC_Time);
$time = strtotime($utc[1]);
date_default_timezone_set("Asia/Calcutta");
$time += $ts3; // Add the difference
return $utc[0]." ".date("H:i:s", $time);
}