【问题标题】:Converting GMT time to local time using timezone offset in php使用php中的时区偏移将GMT时间转换为本地时间
【发布时间】:2014-02-10 06:18:48
【问题描述】:

我需要根据当前时区显示用户的活动日期。 我的方法-

  1. 从 javascript 获取时区偏移量并将其存储到用户的配置文件表中。
  2. 当用户登录时,获取时区偏移量。
  3. 当前日期在时区偏移下工作正常-

$offsetDiff = $_SESSION['TimeZone']*60;

$UserDateTime = time() + $offsetDiff;

$currentDate = date('Y-m-d',$UserDateTime);

  1. 今天以外的 Dateo 无法正常工作 -

$offsetDiff = $_SESSION['TimeZone']*60;

$UserDateTime = '2014-02-10 08:58:00'; + $offsetDiff;

$monthUser = date('Y-m-d',$UserDateTime);

谁能告诉我如何根据时区偏移显示正确的日期?

【问题讨论】:

标签: php timestamp-with-timezone


【解决方案1】:

您可以将特定偏移量转换为 DateTimeZone:

$offset = '-0500';
$isDST = 1; // Daylight Saving 1 - on, 0 - off
$timezoneName = timezone_name_from_abbr('', intval($offset, 10) * 36, $isDST);
$timezone = new DateTimeZone($timezoneName);

然后你可以在 DateTime 构造函数中使用它,例如

$datetime = new DateTime('2012-04-21 01:13:30', $timezone);

或使用 setter:

$datetime->setTimezone($timezone);

在后一种情况下,如果 $datetime 使用不同的时区构造,则日期/时间将转换为指定的时区。

【讨论】:

猜你喜欢
  • 2012-05-02
  • 2012-07-15
  • 2013-10-29
  • 2012-05-01
  • 2014-10-25
  • 1970-01-01
  • 2010-09-15
相关资源
最近更新 更多