php:CI中的夏令时(伦敦)问题


daylight saving (London) issue in php: CI

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

我的时间是:2016年7月4日下午03:00预计伦敦时间是:2016月7日上午10:30

在我的情况下是:2016年4月7日上午09:30

以下是我在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='亚洲/加尔各答';

如果您想将当地时间转换为伦敦时间,请使用"欧洲/伦敦"。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' ) );

如果需要伦敦时间,请将时区设置为"欧洲/伦敦",而不是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"