Php codeigniter中的日期助手时区菜单功能
我正在使用Php codeigniter中的日期助手时区菜单功能,php,codeigniter,timezone,Php,Codeigniter,Timezone,我正在使用timezone\u菜单功能,它工作得非常好 我存储时区数据库,并以您配置我的客户端的格式在web应用程序中显示日期和时间 我的问题是,现在我有一个客户危地马拉,它告诉我你的时区和墨西哥中心不一样。有一个时区-6(墨西哥中部标准时间中心),但表示一年中的某个时间与危地马拉的时间不一致,因为危地马拉是墨西哥的中心 例如,在Windows中,在段落时区中,我的客户使用了中美洲而不是瓜达拉哈拉、墨西哥城、蒙特雷。有人能帮我吗?我认为您遇到了一个问题,因为您的时区列表缺少存储所需数据所需的粒度
timezone\u菜单
功能,它工作得非常好
我存储时区数据库,并以您配置我的客户端的格式在web应用程序中显示日期和时间
我的问题是,现在我有一个客户危地马拉,它告诉我你的时区和墨西哥中心不一样。有一个时区-6(墨西哥中部标准时间中心),但表示一年中的某个时间与危地马拉的时间不一致,因为危地马拉是墨西哥的中心
例如,在Windows中,在段落时区中,我的客户使用了中美洲而不是瓜达拉哈拉、墨西哥城、蒙特雷。有人能帮我吗?我认为您遇到了一个问题,因为您的时区列表缺少存储所需数据所需的粒度 我会说,规范化每个时区条目,因此:为每个可能的时区创建一个时区条目,将其存储在数据库中,并从数据库中呈现选项。然后,简单地将用户的时区存储在他们的设置中 然后,您可以轻松地从一个时区转换到另一个时区。将所有日期时间存储为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;
}
}
模型:
$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;
}
}