Mar*_*ark 29 php timezone datetime
我使用以下方法将UTC日期存储到数据库中:
$utc = gmdate("M d Y h:i:s A");
Run Code Online (Sandbox Code Playgroud)
然后我想将保存的UTC日期转换为客户端的本地时间.
我怎样才能做到这一点?
谢谢
web*_*rgm 61
PHP的strtotime
功能将解释时区代码,如UTC.如果您从没有时区代码的数据库/客户端获取日期,但知道它是UTC,那么您可以附加它.
假设您获得带有时间戳代码的日期(例如"Fri Mar 23 2012 22:23:03 GMT-0700(PDT)",这是Javascript代码""+(new Date())
给出的):
$time = strtotime($dateWithTimeZone);
$dateInLocal = date("Y-m-d H:i:s", $time);
Run Code Online (Sandbox Code Playgroud)
或者如果你不这样做,那可能来自MySQL,那么:
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
Run Code Online (Sandbox Code Playgroud)
Tim*_*nen 50
将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');
Run Code Online (Sandbox Code Playgroud)
time()
返回unix时间戳,这是一个数字,没有时区.
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');
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到所有可用的时区
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
Run Code Online (Sandbox Code Playgroud)
如果您只想在不同时区显示相同的时间戳,则不需要日期算术:
$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);
Run Code Online (Sandbox Code Playgroud)
date()
并且localtime()
都使用了,除非覆盖了服务器的本地时区; 你可以覆盖使用的时区date_default_timezone_set()
.
http://www.php.net/manual/en/function.date-default-timezone-set.php
http://us3.php.net/manual/en/function.date.php
http://php.net/manual/en/function.localtime.php
首先,以UTC格式获取日期 - 您已经完成了这一步,因此这一步实际上只是一个数据库调用:
$timezone = "UTC";
date_default_timezone_set($timezone);
$utc = gmdate("M d Y h:i:s A");
print "UTC: " . date('r', strtotime($utc)) . "\n";
Run Code Online (Sandbox Code Playgroud)
接下来,在PHP中设置本地时区:
$timezone = "America/Guayaquil";
date_default_timezone_set($timezone);
Run Code Online (Sandbox Code Playgroud)
现在得到几秒钟的偏移:
$offset = date('Z', strtotime($utc));
print "offset: $offset \n";
Run Code Online (Sandbox Code Playgroud)
最后,将偏移量添加到原始日期时间的整数时间戳:
print "LOCAL: " . date('r', strtotime($utc) + $offset) . "\n";
Run Code Online (Sandbox Code Playgroud)
小智 5
我将日期以UTC格式存储在数据库中,但随后将其显示给本地用户的本地时区
// retrieve
$d = (new \DateTime($val . ' UTC'))->format('U');
return date("Y-m-d H:i:s", $d);
Run Code Online (Sandbox Code Playgroud)
这是将提问者的 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
Run Code Online (Sandbox Code Playgroud)
然后,您可以将此差异添加到存储的 UTC 时间。(在我的地方,雅典,时差正好是 5:00:00)
例子:
$time = time() // Or any other timestamp
$time += $ts3 // Add the difference
$dateInLocal = date("Y-m-d H:i:s", $time);
Run Code Online (Sandbox Code Playgroud)
小智 5
我在这里共享脚本,将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');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
136987 次 |
最近记录: |