在PHP中将UTC日期转换为本地时间

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)

  • 第二个代码片段是我需要将sqlite的`datetime()`函数的输出从UTC转换为localtime所需的代码片段. (3认同)

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)

  • 这实际上回答了问题,而不是提供“如何获取当地时间”。 (2认同)
  • 这是正确的答案,因为更改 $dt 对象的 TimeZone 正确地保留了对象时间的夏令时偏移量(而不是执行代码时的本地当前时间) (2认同)

fij*_*ron 9

如果您只想在不同时区显示相同的时间戳,则不需要日期算术:

$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)


Amb*_*ber 8

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

  • date(),localtime()等不回答问题,因为问题的关键词是"UTC",这里缺少.问题确实非常简单:如何将UTC时间转换为当地时间.(*任何*时间,而不是当前时间.)请参阅我的回复. (4认同)
  • 但是如何获取当前的UTC时间戳? (3认同)

fij*_*ron 7

首先,以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)


Apo*_*los 5

这是将提问者的 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)