Codeigniter,timezone_menu和date_default_timezone_set [英] Codeigniter, timezone_menu and date_default_timezone_set

查看:126
本文介绍了Codeigniter,timezone_menu和date_default_timezone_set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Codeigniters timezone_menu ,它提供了下拉框时区时区,我想知道我们应该如何利用这些时区与PHP date_default_timezone_set

I make use of Codeigniters timezone_menu which gives a drop down box of time timezones and I am wondering how are we supposed to make use of these timezones with PHPs date_default_timezone_set?

codeigniters时区的示例为 UP1 。但是,你不能使用PHP date_default_timezone_set 作为字符串,它应该是像 Europe / Berlin

An example of codeigniters timezone is UP1. However, you can't use that with PHPs date_default_timezone_set as the string it takes in should be something like Europe/Berlin

问题是,有没有办法将codeigniter时区字符串转换为PHP可以接受 date_default_timezone_set

The question is, is there a way to convert a codeigniter timezone string to a PHP one that can be accepted by the date_default_timezone_set?

推荐答案

以下适用于我:

// Example - timezones is part of the date helper function
// You obviously wouldn't echo it out, you would pass it in to 
// set default timezone function in PHP based on the posted data from 
// the dropdown
echo timezone_by_offset(timezones('UM5')); // ouputs 'America/Porto_Acre' which is (-5)

// And the function
function timezone_by_offset($offset) {
    $offset = ($offset+1) * 60 * 60;
    $abbrarray = timezone_abbreviations_list();

    foreach ($abbrarray as $abbr) {
        foreach ($abbr as $city) {
            if ($city['offset'] == $offset) { 
                echo($city['timezone_id']);
                return true;
            }
        }
    }
    echo "UTC";
    return false;
} 

EDIT
原函数didn不排除DST偏移。 timezone_abbreviations_list()为DST标记为true的时区列出2x,例如Tasmania出现在+11和+10下。因此,如果DST == TRUE,则忽略它作为返回列表的一部分。

EDIT The original function didn't exclude DST offsets. timezone_abbreviations_list() lists 2x for a timezone that has DST marked as true, for example Tasmania appears under +11 and +10. So if DST == TRUE, ignore it as part of the return listing.

//-- Function -------------------------------------------------------------------
function timezone_by_offset($offset) {
    $abbrarray = timezone_abbreviations_list();
    $offset = $offset * 60 * 60;

    foreach ($abbrarray as $abbr) {
        foreach ($abbr as $city) {
            if ($city['offset'] == $offset && $city['dst'] == FALSE) { 
                return $city['timezone_id'];                                
            }
        }
    }
    return 'UTC'; // any default value you wish
}

一些例子:

//-- Test Cases -------------------------------------------------------------------
echo timezone_by_offset(-12) . '<br/>';  
echo (date_default_timezone_set(timezone_by_offset(-12)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; 
// Etc/GMT+12
// Valid 

echo timezone_by_offset(-10) . '<br/>';  
echo (date_default_timezone_set(timezone_by_offset(-10)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; 
// America/Anchorage
// Valid

echo timezone_by_offset(-8) . '<br/>';  
echo (date_default_timezone_set(timezone_by_offset(-8)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; 
// Etc/GMT+8
// Valid        

echo timezone_by_offset(6) . '<br/>';  
echo (date_default_timezone_set(timezone_by_offset(6)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; 
// Asia/Aqtobe
// Valid        

echo timezone_by_offset(7) . '<br/>';  
echo (date_default_timezone_set(timezone_by_offset(7)) == TRUE ? 'Valid' : 'Not Valid'). '<br/>'; 
// Indian/Christmas
// Valid

这篇关于Codeigniter,timezone_menu和date_default_timezone_set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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