Php 选择时更改时区

Php 选择时更改时区,php,mysql,kohana,Php,Mysql,Kohana,是否可以使用不同的时区从数据库中进行选择? 我的表格中有一些记录,它们的时间列是GMT时区的时间戳。 例如,我想获得30-05-2013的所有记录。使用PST时区 我在谷歌上搜索了abit,发现如果我在运行这个查询: SET time_zone = 'America/Los_Angeles'; 在每次查询之前,它都应该起作用(如果我错了,请纠正我) 但我能用Kohana ORM做什么 这是我的模型: 不容易但可行(但未经测试)。 1.您应该在数据库类中添加get\u connection()

是否可以使用不同的时区从数据库中进行选择? 我的表格中有一些记录,它们的
时间
列是GMT时区的时间戳。
例如,我想获得
30-05-2013
的所有记录。使用PST时区

我在谷歌上搜索了abit,发现如果我在运行这个查询:

SET time_zone = 'America/Los_Angeles';
在每次查询之前,它都应该起作用(如果我错了,请纠正我)

但我能用Kohana ORM做什么

这是我的模型:

不容易但可行(但未经测试)。
1.您应该在
数据库
类中添加
get\u connection()
方法(将其直接放在
应用程序/classes/
文件夹中,文件名应该是
Database.php
):

二,。您应该在模型中重写
\uu construct()
方法:

class Model_Calls extends ORM {
public function __construct($id = NULL, $timezone = NULL)
{
    $this->_initialize();

    // ADDED
    if ($timezone)
    {
        $this->_db->get_connection()->exec('SET time_zone = ' . $this->_db->quote($timezone));
    }
    // END ADDED

    if ($id !== NULL)
    {
        if (is_array($id))
        {
            foreach ($id as $column => $value)
            {
                // Passing an array of column => values
                $this->where($column, '=', $value);
            }

            $this->find();
        }
        else
        {
            // Passing the primary key
            $this->where($this->_object_name . '.' . $this->_primary_key, '=', $id)->find();
        }
    }
    elseif (!empty($this->_cast_data))
    {
        // Load preloaded data from a database call cast
        $this->_load_values($this->_cast_data);

        $this->_cast_data = array();
    }
}
现在,您可以按如下方式加载记录:

$res = ORM::factory('call', NULL, 'America/Los_Angeles')->get_all_calls_I_need(xxxxx);
请注意,当您使用查询生成器调用时,这将不起作用。

在这种情况下,在执行正确的查询之前,您需要手动设置时区。

这就足够运行
set time\u zone='America/Los\u Angeles'在您建立连接后立即执行。@claustrofob-感谢您的快速响应。你知道我应该把这个调用添加到Kohana框架的什么地方吗?不幸的是,我不熟悉Kohana。有人一定会帮你的。那么你的
Model_调用
的所有条目都在GMT中,而其他所有的机型都在PST中?我刚刚查看了ORM的源文件,在
\u initialize()
中,您获得了db连接。您需要知道一个模型何时完成了所有任务,因为db是作为单例实现的。但是,您可以创建一个使用所需时区的新实例,然后修改某些模型的
\u initialize()
方法
class Model_Calls extends ORM {
public function __construct($id = NULL, $timezone = NULL)
{
    $this->_initialize();

    // ADDED
    if ($timezone)
    {
        $this->_db->get_connection()->exec('SET time_zone = ' . $this->_db->quote($timezone));
    }
    // END ADDED

    if ($id !== NULL)
    {
        if (is_array($id))
        {
            foreach ($id as $column => $value)
            {
                // Passing an array of column => values
                $this->where($column, '=', $value);
            }

            $this->find();
        }
        else
        {
            // Passing the primary key
            $this->where($this->_object_name . '.' . $this->_primary_key, '=', $id)->find();
        }
    }
    elseif (!empty($this->_cast_data))
    {
        // Load preloaded data from a database call cast
        $this->_load_values($this->_cast_data);

        $this->_cast_data = array();
    }
}
$res = ORM::factory('call', NULL, 'America/Los_Angeles')->get_all_calls_I_need(xxxxx);