mysql - Codeigniter 时区 mysql 设置

标签 mysql codeigniter timezone

刚刚意识到为什么我的网站现在将所有日期时间变量显示为 -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 列值。 View 中的回显变量总是比我数据库中的值少 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'");在该模型构造函数中。

config/autoload.php

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

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

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

在每次加载页面时,都会调用此构造函数并将 mysql 时区设置为 +0:00。

关于mysql - Codeigniter 时区 mysql 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426887/

相关文章:

mysql - SQL选择并集的多个项目

mysql - 如何使用 MySQL 在变量中声明表名?

php - CodeIgniter 基本 Controller 扩展

javascript - 将 JavaScript 日期从一个时区转换为另一时区?

python - 如何将带有时区字符串的字符串转换为日期时间?

mysql - 成员表包含一个而不是三个唯一字段

c# - Context.User 在 Asp.net MVC 应用程序中通过 windows auth 在 Application_AuthenticateRequest 中为 NULL

mysql - Cron 作业检查数据库中的日期并发送通知

php - MySQL 使用 codeIgniter active query 或 alt 搜索所有文件。转义(像+等于)

python - 在 Python 中检查当前时间是否小于特定时间?