2013-07-02 46 views
5

刚刚意识到为什么我的网站现在显示所有日期时间变量为-1小时......我第一次使用Codeigniter! (以前从未出现过这种问题)Codeigniter时区mysql设置

所以,我已经包含在我的主index.php文件下面的代码

/* 
|--------------------------------------------------------------- 
| DEFAULT TIMEZONE 
|--------------------------------------------------------------- 
| 
| Set the default timezone for date/time functions to use if 
| none is set on the server. 
| 
*/ 

if(! ini_get('date.timezone')) 
{ 
    date_default_timezone_set('GMT'); 
} 

但是,它仍然显示为-1小时,所以我假设我需要设置某种对MySQL默认设置的...

我已经包括下面的代码行中我的模型:

function __construct() 
    { 
     // Call the Model constructor 
     parent::__construct(); 
     $this->db->query("SET time_zone='+0:00'"); 
    } 

仍然没有区别...帮助!

我的代码是:

<h3><?=date('D, jS F @ g:ia', strtotime($row->datetime))?></h3> 

的$按行>日期时间变量无非是从我的MySQL数据库中的DATETIME列值更。鉴于回送的变量总是比我的数据库中值少1小时...

我的模型代码是:

function coming_up() 
{ 
    $this->db->query("SET time_zone='+0:00'"); 
$query = $this->db->query('SELECT * FROM events1 WHERE datetime >= NOW() ORDER BY datetime LIMIT 2'); 
return $query->result(); 
} 

回答

2

config file添加这些行,然后再检查,这是为我工作

$config['time_reference'] = 'gmt';# Default should be GMT 
date_default_timezone_set('UTC');# Add this line after creating timezone to GMT for reflecting 
+0

没有改变我怕...... @Rohan库马尔 – user2505513

+0

检查'插入query'它应该看起来像'$ this-> db-> query(“INSERT INTO TABLE SET time_zone ='+ 0:00'”);' –

+0

请参阅上文,我编辑了我的帖子以包含我的查询代码@Rohan Kumar仍然没有区别! – user2505513

11

在config/autoload.php中,设置一个模型以加载每个页面加载。然后调用$ this-> db-> query(“SET time_zone ='+ 0:00'”);在那个模型构造函数中。

配置/ autoload.php

$自动加载[ '模型'] =阵列( 'default_model'); //用于离, “说 default_model”

在应用/模型,创建一个名称为“default_model.php”的新模型文件并添加以下代码。

应用程序/模型/ default_model.php

类Default_model扩展CI_Model {

function __construct() 
{ 
    // Call the Model constructor 
    parent::__construct(); 
    $this->db->query("SET time_zone='+0:00'"); 
} 
    } 

在每个页面加载,这个构造函数将被调用,mysql的时区将设置为+ 0:00。

+0

如果你需要处理DST,你可以加载命名区'$ this-> db-> query(“SET time_zone ='America/Chicago'”);'** note **你需要有MySQL时区设置[mysql_tzinfo_to_sql](http://dev.mysql.com/doc/refman/5.1/en/mysql-tzinfo-to-sql.html) – zerodahero

+0

这真是太棒了,谢谢! –

+0

将'date_default_timezone_set('TIMEZONE_SET_FOR_DATABASE');'添加到'defined'('BASEPATH')行下方的application/config/config.php或'exit'('不允许直接脚本访问')。 ');' –

-4

您可以设置它在你的项目文件夹中的index.php文件在顶部,后<?php

<?php 
date_default_timezone_set('Asia/Bangkok');