本文 17221 pv

2

CodeIgniter时区设置

© kekehu / 技术资源 / 2010.12.30 / 16:54 / 17221PV

Codeigniter框架时区设置两种方法
1.用now()得到gmt时间,这个前提前条是要在config.php文件中time reference设定返回GMT时间。
  当然你也可以用local_to_gmt()得到GMT时间。
  用timezone_menu()生成一个时区下拉选单,根据用户选择时区设置的值,返回当前时间。
  例如:
gmt_to_local(now(), $config['timezones']) 
//或 gmt_to_local(local_to_gmt(), $config['timezones'])
//$config['timezones']是用户选择时区的值。

2.codeigniter中提供了一份较为完整的时区列表,timezone_menu() 可以显示一个时区的下拉列表,但是这个列表中的时间不能完全对应到PHP自带的时区显示上,这是PHP本身的问题,不过可以通过下面这个函数,来让输入的每个时区,都可以获得一个对应的时区国家名称,这样就可以用date_default_timezone_set函数来设置时区了。
$timezone = tz_offset_to_name('UP8');
if(function_exists('date_default_timezone_set'))
{ 
  date_default_timezone_set($timezone); 
}

if( ! function_exists("tz_offset_to_name") ) 
{ 
    /* Takes a GMT offset (in hours) and returns a timezone name */ 
    function tz_offset_to_name($offset) 
    { 
            $offset *= 3600; // convert hour offset to seconds 
            $abbrarray = timezone_abbreviations_list(); 
            foreach ($abbrarray as $abbr) 
            { 
                    foreach ($abbr as $city) 
                    { 
                            if ($city['offset'] == $offset) 
                            { 
                                    return $city['timezone_id']; 
                            } 
                    } 
            } 
            return FALSE; 
    } 
}

本文有 2 篇评论

  1. @ 丰禾 Email 说 :
    2011.02.12 10:42
    avatar

    不错呢。新年愉快么?

  2. @ 丰禾 Email 说 :
    2011.01.22 14:39
    avatar

    不错  谢谢分享。

1/1 第一页 1 最后页

发表你的见解

打开HTML 打开UBB 打开表情 隐藏 记住我
emotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemot