PHP 将 UTC 时间转换为本地时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23926250/
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
PHP convert UTC time to local time
提问by Dibish
Am getting a UTC time from my server like the following format, my requirement is to convert the UTC time to local time. So users can see a user friendly time on their browser based on their timezone. Please help me to solve this issue. Thank you
我从我的服务器获取 UTC 时间,如下格式,我的要求是将 UTC 时间转换为本地时间。所以用户可以根据他们的时区在他们的浏览器上看到一个用户友好的时间。请帮我解决这个问题。谢谢
$utc = "2014-05-29T04:54:30.934Z"
I have tried some methods but not working in my case
我尝试了一些方法,但在我的情况下不起作用
First
第一的
$time = strtotime($utc);
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;
Second
第二
$time = strtotime($utc .' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;
回答by Phil
Simply use a DateTimeZone
, eg
只需使用 a DateTimeZone
,例如
$dt = new DateTime($utc);
$tz = new DateTimeZone('Asia/Kolkata'); // or whatever zone you're after
$dt->setTimezone($tz);
echo $dt->format('Y-m-d H:i:s');
Demo ~ http://ideone.com/fkM4ct
回答by Martin Konecny
The reason your code isn't working is most likely because your server is in UTC time. So the local time of the server isUTC.
您的代码无法正常工作的原因很可能是因为您的服务器处于 UTC 时间。所以服务器的本地时间是UTC。
Solution #1
解决方案#1
One potential solution is to do the following server side and pass the epoch integer to the browser:
一种可能的解决方案是执行以下服务器端并将纪元整数传递给浏览器:
$utc = "2014-05-29T04:54:30.934Z";
$time = strtotime($utc); //returns an integer epoch time: 1401339270
Then use JavaScript to convert the epoch integer to the user's local time (the browser knows the user's timezone).
然后使用 JavaScript 将纪元整数转换为用户的本地时间(浏览器知道用户的时区)。
Solution #2
解决方案#2
You can get the browser to send you the user's timezone. Then you can use this information to calculate the date string server side instead of browser side. See more here: https://stackoverflow.com/a/5607444/276949
您可以让浏览器向您发送用户的时区。然后您可以使用此信息来计算服务器端而不是浏览器端的日期字符串。在此处查看更多信息:https: //stackoverflow.com/a/5607444/276949
This will give you the user's offset (-7 hours). You can use this information to set the timezone by looking here: Convert UTC offset to timezone or date
这将为您提供用户的偏移量(-7 小时)。您可以通过查看此处使用此信息来设置时区:Convert UTC offset to timezone or date
回答by Robin
If this is not done via a user profile setting, it is probably easier and simpler to use a javascript time library eg. moment.js (http://momentjs.com/) to solve this problem (send all date/times in UTC and then convert them on the client end).
如果这不是通过用户配置文件设置完成的,使用 javascript 时间库可能更容易和更简单,例如。moment.js ( http://momentjs.com/) 来解决这个问题(以 UTC格式发送所有日期/时间,然后在客户端进行转换)。
Javascript eg. (I am sure this can be achieved without creating two moment objects (fix that at your leisure).
Javascript 例如。(我相信这可以在不创建两个矩对象的情况下实现(在您闲暇时修复它)。
function utcToLocalTime(utcTimeString){
var theTime = moment.utc(utcTimeString).toDate(); // moment date object in local time
var localTime = moment(theTime).format('YYYY-MM-DD HH:mm'); //format the moment time object to string
return localTime;
}
php example for server side resolution (only use one of these solutions, both together will produce an incorrect time on the client end)
用于服务器端解析的 php 示例(仅使用这些解决方案之一,两者一起会在客户端产生错误的时间)
/**
* @param $dateTimeUTC
* @param string $timeZone
* @param string $dateFormat
* @return string
*
* epoch to datetime (utc/gmt)::
* gmdate('Y-m-d H:i:s', $epoch);
*/
function dateToTimezone($timeZone = 'UTC', $dateTimeUTC = null, $dateFormat = 'Y-m-d H:i:s'){
$dateTimeUTC = $dateTimeUTC ? $dateTimeUTC : date("Y-m-d H:i:s");
$date = new DateTime($dateTimeUTC, new DateTimeZone('UTC'));
$date->setTimeZone(new DateTimeZone($timeZone));
return $date->format($dateFormat);
}
回答by Divyesh
#cnvt_usrTime_to_UTC 0
function cnvt_usrTime_to_UTC($dt_start_time_formate,$UTC_TimeZone){
$LocalTime_start_time = new DateTime( $dt_start_time_formate );
$tz_start = new DateTimeZone( $UTC_TimeZone );
$LocalTime_start_time->setTimezone( $tz_start );
$array_start_time = (array) $LocalTime_start_time;
return $UTC_Time_Start_Time = $array_start_time['date'];
}
call the function cnvt_usrTime_to_UTC($dt_start_time_formate,'UTC');
调用函数 cnvt_usrTime_to_UTC($dt_start_time_formate,'UTC');
#cnvt_UTC_to_usrTime
function cnvt_UTC_to_usrTime($Date_Time,$User_time_Zone){
date_default_timezone_set('UTC');
$LocalTime_start_time = new DateTime( $Date_Time );
$tz_start = new DateTimeZone( $User_time_Zone );
$LocalTime_start_time->setTimezone( $tz_start );
$start_date_time = (array) $LocalTime_start_time;
return $StartDateTime = $start_date_time['date'];
}
call the function
调用函数
cnvt_UTC_to_usrTime($value_edit['dt_start_time'],Asia/Kolkata);
回答by Matt Johnson-Pint
Just send the UTC time to the browser. Use JavaScript to parse it with a Date
object, or with moment.js, then output it as text.
只需将 UTC 时间发送到浏览器。使用 JavaScript 用Date
对象或moment.js解析它,然后将其输出为文本。
By doing it in client-side JavaScript, you don't need to be aware of the user's time zone. The browser will be responsible for that instead.
通过在客户端 JavaScript 中执行此操作,您无需知道用户的时区。浏览器将对此负责。
回答by Girish
Use function date_default_timezone_set
before there you want to local time after that again setup UTC Europe/Lisbon
timezone
List of Supported Timezones
date_default_timezone_set
在您想要本地时间之前使用功能,然后再次设置UTCEurope/Lisbon
时区
List of Supported Timezones
<?php
$utc = "2014-05-29T04:54:30.934Z";
$time = strtotime($utc);
echo "<br/>Defualt UTC/server time".$dateInLocal = date("Y-m-d H:i:s", $time);
//your local time zone put here
date_default_timezone_set('Asia/Kolkata');
echo "<br/>Local time". $dateInLocal = date("Y-m-d H:i:s", $time);
?>
回答by Rakesh Sharma
try with date_default_timezone_set()and set date according timezone
尝试使用date_default_timezone_set()并根据时区设置日期
date_default_timezone_set('asia/kolkata');
$utc = "2014-05-29T04:54:30.934Z";
$time = strtotime($utc);
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal; //2014-05-29 10:24:30
echo '<br>';
date_default_timezone_set('America/Chicago');
$time = strtotime($utc .' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal; //2014-05-28 23:54:30
回答by Nitesh Sharma
$savedtime = '2019-05-25 00:01:13'; //this is America/Chicago time
$servertime = ini_get('date.timezone');
$time = strtotime($savedtime . $servertime);
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;
display value according to local timezone
2019-05-25 10:31:13 // this is Asia/Kolkata time