繁体   English   中英

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

[英]Convert UTC dates to local time in PHP

我使用以下方法将 UTC 日期存储到数据库中:

$utc = gmdate("M d Y h:i:s A");

然后我想将保存的 UTC 日期转换为客户端的本地时间。

我怎样才能做到这一点?

谢谢

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

或者,如果您不这样做,这可能来自 MySQL,那么:

$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);

回答

将 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 its time
$dt->setTimezone(new DateTimeZone('America/Denver'));

// format the datetime
$dt->format('Y-m-d H:i:s T');

这适用于 2032 年之后的日期、夏令时和闰秒,并且不依赖于主机语言环境或时区。

它使用 timezonedb 进行计算,这个 db 随着时区规则的变化而随着时间的推移而变化,并且必须更新。

笔记

time()返回unix timestamp ,它是一个数字,它没有时区。

date('Ymd H:i:s T')返回当前语言环境时区的日期。

gmdate('Ymd 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

由于夏令时,某些日期在某些时区重复,例如,在美国,2011 年 3 月 13 日凌晨 2 点 15 分从未发生,而 2011 年 11 月 6 日凌晨 1 点 15 分发生了两次。 无法准确确定这些日期时间。

我在这里分享脚本,将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');

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

$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 日期——您已经这样做了,所以这一步实际上只是一个数据库调用:

$timezone = "UTC";
date_default_timezone_set($timezone);

$utc = gmdate("M d Y h:i:s A");
print "UTC: " . date('r', strtotime($utc)) . "\n";

接下来,在 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";

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 格式将日期存储在数据库中,然后我将它们显示给本地时区的最终用户

// retrieve
$d = (new \DateTime($val . ' UTC'))->format('U');
return date("Y-m-d H:i:s", $d);

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

示例:

$time = time() // Or any other timestamp
$time += $ts3  // Add the difference
$dateInLocal = date("Y-m-d H:i:s", $time);

给定本地时区,例如“美国/丹佛”,您可以使用 DateTime 类将 UTC 时间戳转换为本地日期:

$timestamp = *********;
$date = new DateTime("@" . $timestamp);
$date->setTimezone(new DateTimeZone('America/Denver'));
echo $date->format('Y-m-d H:i: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);
}

在一行中将UTC 中的日期转换为本地时间或其他时区。

UTC 时间:“2021-09-13 16:30:16”

输出时区:'美国/哈瓦那'

(new DateTime('2021-09-13 16:30:16', new DateTimeZone('UTC')))
->setTimezone(new DateTimeZone('America/Havana'))
->format('Y-m-d H:i:s')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM