0

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

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

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

$UserDateTime = time() + $offsetDiff;

$currentDate = date('Ymd',$UserDateTime);

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

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

$UserDateTime = '2014-02-10 08:58:00'; + $偏移量;

$monthUser = date('Ymd',$UserDateTime);

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

4

1 回答 1

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

或使用二传手:

$datetime->setTimezone($timezone);

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

于 2014-02-10T06:27:41.743 回答