使用时区偏移将GMT时间转换为本地时间,而不是时区标识 [英] Converting GMT time to local time using timezone offset, not timezone identifier

查看:261
本文介绍了使用时区偏移将GMT时间转换为本地时间,而不是时区标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您从PHP列表中获取时区标识符,那么将给定的GMT日期转换为本地时间是很容易的: http://www.php.net/manual/en/timezones.php



例如,你可以做这里($ fromTimeZone只是'GMT',$ toTimeZone只是列表中的一个常量(即'America / Chicago'),而$ datetime是GMT日期):

  public static function convertToTimezone($ datetime,$ fromTimeZone,$ toTimeZone,$ format ='Ymd H:i')
{
//构造一个新的DateTime对象从给定的时间,设置在原始时区
$ convertedDateTime = new DateTime($ datetime,timezone_open($ fromTimeZone));
//将发布的日期转换为新的时区
$ convertedDateTime-> setTimezone(timezone_open($ toTimeZone));
//以给定的格式返回udpated日期
return $ convertedDateTime-> format($ format);
}

但是,我有问题将相同的GMT日期转换为当地时间如果刚刚给出时区偏移。例如,而不是被赋予美国/芝加哥,我被赋予-0500(这是该时区的等效偏移量)。



我已经尝试过了例如以下(其中$ datetime是我的GMT日期,$ toTimeZone是偏移量(在这种情况下为-0500)):

  date($ format,strtotime($ datetime。'。$ toTimeZone))

我知道所有date()类的函数是基于服务器的时区。我似乎无法让它忽略,并使用明确给出的时区偏移。

解决方案

你可以转换对DateTimeZone的具体偏移量:

  $ offset ='-0500'; 
$ isDST = 1; //夏令时1 - 开,0 - 关
$ 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 使用不同的时区构建,日期/时间将转换为指定的时区。


It's pretty easy to convert a given GMT date into local time if you're given the timezone identifier from this list in PHP: http://www.php.net/manual/en/timezones.php

For example, you can do this (where $fromTimeZone is just 'GMT', $toTimeZone is just one of the constants from that list (i.e. 'America/Chicago'), and $datetime is the GMT date):

public static function convertToTimezone($datetime, $fromTimeZone, $toTimeZone, $format = 'Y-m-d H:i')
{
    // Construct a new DateTime object from the given time, set in the original timezone
    $convertedDateTime = new DateTime($datetime, timezone_open($fromTimeZone));
    // Convert the published date to the new timezone
    $convertedDateTime->setTimezone(timezone_open($toTimeZone));
    // Return the udpated date in the format given
    return $convertedDateTime->format($format);
}

However, I'm having issue converting the same GMT date to the local time if just given the timezone offset. For instance, instead of being given 'America/Chicago', I'm given -0500 (which is the equivalent offset for that timezone).

I've tried things such as the following (where $datetime is my GMT date and $toTimeZone is the offset (-0500 in this case)):

date($format, strtotime($datetime . ' ' . $toTimeZone))

I know all the date() sort of functions are based on the servers's timezone. I just can't seem to get it to ignore that and use a timezone offset that is given explicitly.

解决方案

You can convert a specific offset to a 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);

Then you can use it in a DateTime constructor, e.g.

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

or with the setter:

$datetime->setTimezone($timezone);

In the latter case, if $datetime was constructed with a different timezone, the date/time will be converted to specified timezone.

这篇关于使用时区偏移将GMT时间转换为本地时间,而不是时区标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆