【问题标题】:daylight saving (London) issue in php: CIphp 中的夏令时(伦敦)问题:CI
【发布时间】:2016-07-28 04:01:23
【问题描述】:

我正在尝试将任何时区转换为伦敦时区。目前不考虑日光概念。

我的时间是:07-04-2016 03:00 PM预计在伦敦是:07-04-2016 10:30 AM

在我的情况下是:07-04-2016 09:30 AM

这是我在 CI Helper 中的 php 代码:

function convert_datetime_to_utc_timezone($date, $timezone) {

    if ($date != '') {
        $ci = & get_instance();
        $dformat .= "Y-m-d H:i:s";
        $zone = get_list_of_all_timezone();

        $user_time_zone = $zone[$timezone];

        $convert_date = new DateTime($date, new DateTimeZone($user_time_zone));
        $convert_date->setTimeZone(new DateTimeZone('UTC'));
        return $convert_date->format('Y-m-d H:i:s');
    } else {
        return '';
    }
}

注意: $user_time_zone = 'Asia/Calcutta';

【问题讨论】:

  • 伦敦应该有 GMT+1 区
  • 需要更改代码吗?请建议!
  • 代码库是永久的,所以它应该在伦敦时区没有夏令时时工作,反之亦然!
  • 你在 localhost 中测试这个吗?
  • 不,它是实时的。实际上我正在使用easycron,他们的时区是伦敦。

标签: php codeigniter timezone dst


【解决方案1】:

如果您想将本地时间转换为伦敦时间,请使用“欧洲/伦敦”。 UTC、EST 等一般时区不会考虑夏令时。

$your_date = date ( 'Y-m-d H:i:s', strtotime ( $your_date ) );

$newyork_time = new DateTime ( $your_date, new DateTimeZone ( 'America/New_York' ) );

$london_time = new DateTime ( $your_date, new DateTimeZone ( 'Europe/London' ) );

【讨论】:

    【解决方案2】:

    如果您想要伦敦时间,请将时区设置为“欧洲/伦敦”,而不是 UTC。

    $ php -a
    Interactive mode enabled
    
    php > $format = 'd-m-Y H:i A';
    php > $input_timezone = 'Asia/Calcutta';
    php > $input_datetime = '07-04-2016 03:00 PM';
    php > $datetime = DateTime::createFromFormat($format, $input_datetime, new DateTimeZone($input_timezone));
    php > $datetime->setTimeZone(new DateTimeZone('Europe/London'));
    php > var_dump($datetime->format($format));
    string(19) "07-04-2016 10:30 AM"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 2010-09-27
      • 2011-02-02
      • 2011-04-07
      • 2017-03-14
      • 2014-09-26
      • 2014-03-23
      相关资源
      最近更新 更多