繁体   English   中英

Codeigniter中的日期助手timezone_menu函数

[英]Date helper timezone_menu function in codeigniter

我正在使用timezone_menu函数,它运行完美。

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

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

例如,在Windows中,在段时区中,对于我的客户而言,使用的是-6时区,而不是中美洲瓜达拉哈拉(Montadrey),墨西哥城瓜达拉哈拉(Guadalajara) 谁能帮我?

我认为您提出了一个问题,因为您所拥有的时区列表缺乏存储所需数据所需的粒度。

我会说,对每个时区条目进行标准化,因此:为每个可能的时区创建一个时区条目,将其存储在数据库中,并从数据库中呈现选项。 那么只需在用户的设置中存储用户的时区即可。

然后,您可以轻松地从一个时区转换为另一个时区。 将所有日期时间存储为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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM