ci 设置mysql超时时间,Codeigniter时区mysql设置

Just realised WHY my site is now showing all datetime variables as -1 hr... I'm using Codeigniter for the first time! (Never had this problem before)

So, I have included the following code in my main index.php file

/*

|---------------------------------------------------------------

| 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');

}

However, it's still showing as -1 hr, so I'm assuming I need to set some sort of default setting for MySQL...

I have included the following line of code in my model:

function __construct()

{

// Call the Model constructor

parent::__construct();

$this->db->query("SET time_zone='+0:00'");

}

Still no difference... Help!

My code is:

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

The $row->datetime variable is nothing more than a DATETIME column value from my MySQL database. The echoed variable in view is ALWAYS 1 hour less than the value in my database...

My model code is:

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();

}

解决方案

In config/autoload.php, set a model to load on each page load. then call $this->db->query("SET time_zone='+0:00'"); in that model constructor.

config/autoload.php

$autoload['model'] = array('default_model');// for ex, "say

default_model"

In application/models, create a new model file with name of "default_model.php" and add below code.

application/models/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'");

}

}

On each page load, this constructor will be called and mysql timezone will be set to +0:00.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值