刚刚意识到为什么我的网站现在将所有日期时间变量显示为 -1 小时...我第一次使用 Codeigniter! (以前没遇到过这个问题)
因此,我在主 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>
$row->datetime 变量只不过是我的 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();
}
在 config/autoload.php 中,设置要在每次页面加载时加载的模型。然后调用 $this->db->query("SET time_zone='+0:00'");在该模型构造函数中。
配置/自动加载.php
$autoload['model'] = array('default_model');// for ex, "say default_model"
在 application/models 中,创建一个名为“default_model.php”的新模型文件并添加以下代码。
应用程序/模型/default_model.php
class Default_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->db->query("SET time_zone='+0:00'");
}
}
在每次页面加载时,都会调用此构造函数,并将 mysql 时区设置为 +0:00。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)