【问题标题】:Date helper timezone_menu function in codeignitercodeigniter 中的日期助手 timezone_menu 函数
【发布时间】:2014-02-03 12:55:56
【问题描述】:

我正在使用timezone_menu 函数,它运行良好。

我存储时区数据库并在我的 Web 应用程序中以您为我的客户端配置的格式显示日期和时间。

我的问题是现在我有一个客户危地马拉,它告诉我你的时区与墨西哥中心不同。有一个时区 -6(中部标准时间 - 墨西哥中部),但表示一年中的某些时间与墨西哥中部的危地马拉不重合。

例如,在 Windows 中,在段落时区中,有几个 -6,在我的客户的情况下,使用 Central America 而不是 Guadalajara, Mexico City, Monterrey。谁能帮帮我?

【问题讨论】:

    标签: php codeigniter timezone


    【解决方案1】:

    我认为您遇到了问题,因为您拥有的时区列表缺少存储所需数据所需的粒度。

    我会说,标准化每个时区条目,因此:为每个可能的时区创建一个时区条目,将其存储在数据库中并从数据库中呈现选项。那么在他们的设置中存储用户的时区就很简单了。

    然后您可以轻松地从一个时区转换到另一个时区。将所有日期时间存储为 UTC 偏移量。然后在用户的时区渲染它们。

    eg, $dt = new DateTime ( NULL, new DateTimeZone('UTC'));
    $dt->setTimeZone(new DateTimeZone($this->My_timezones_model->get_tz(12)));
    

    控制者:

    $this->load->model('My_timezones_model');
    echo $this->My_timezones_model->get_select(2);
    

    型号:

    <?php if ( defined('BASEPATH') === FALSE ) exit('No direct script access allowed');
    
    class My_timezones_model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
            $this->create();
        }
    
        function create()
        {
            if( $this->db->table_exists('my_timezones')  == FALSE)
            {
                $this->db->query( "CREATE TABLE `my_timezones` (
                    `tz_id` int unsigned not null auto_increment,
                    `tz_continent` varchar(12) not null comment 'the continent',
                    `tz_city` varchar(14) not null comment 'the city',
                    PRIMARY KEY (`tz_id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
    
                $arr = array();
                $identifiers = DateTimeZone::listIdentifiers();
                foreach ($identifiers as $identifer)
                {
                    $tmp = explode('/',$identifer);
                    if ((isset($tmp[1])) && ($tmp[1]))
                    {
                        $arr[] = array('tz_continent' => $tmp[0],'tz_city'=>$tmp[1]);   
                    }
    
                }
                $this->db->insert_batch('my_timezones',$arr);
            }
        }
        function get_select($selected_tz_id=FALSE)
        {
            // returns a select
            $timezones = $this->db->get('my_timezones')->result();
            $select = '<select>';
            $optgroup = FALSE;
            foreach ($timezones as $timezone)
            {
                if ($optgroup === FALSE )
                {
                    $select .= "<optgroup label='$timezone->tz_continent'>";
                    $optgroup = $timezone->tz_continent;
                }
                if ($timezone->tz_continent != $optgroup)
                {
                    $select .= "</optgroup><optgroup label='$timezone->tz_continent'>";
                    $optgroup = $timezone->tz_continent;
                }
                $select .="<option value='$timezone->tz_id'";
                if ($timezone->tz_id == $selected_tz_id)
                {
                    $select .= " selected='selected' ";
                }
                $select .=">$timezone->tz_city</option>";
            }
            $select .="</optgroup></select>";
            return $select;
        }
    
        function get_continents()
        {
            // returns a list of continents
            return $this->db->select('tz_continent')->group_by('tz_continent')->order_by('tz_sort_order')->get($this->get('table_name'))->result();
        }
        function get_tz($id)
        {
            // input of the tz_id
            // returns a string of the timezone in format: Continent/City, eg Australia/Hobart, or NULL if the ID is not found.
    
            $this->db->select("concat(`tz_continent`,'/',`tz_city`) as tz",FALSE)
                ->from($this->get('table_name'))
                ->where('tz_id',$id);
            $q = $this->db->get();
            $r = NULL;
            if ( $q->num_rows()  == 1 )
            {
                $r = str_replace(' ','_',$q->first_row()->tz);
            }
            return $r;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      相关资源
      最近更新 更多