简体   繁体   English

gmdate()在2个不同的控制器中返回2个不同的结果(codeigniter)

[英]gmdate() returning 2 different results in 2 different controllers (codeigniter)

I have a situation where I am using the php function gmdate() in two different codeigniter controllers. 我有一种情况,我在两个不同的codeigniter控制器中使用PHP函数gmdate()。 The controllers are returning a 6 hour difference with the exact same calls. 控制器与完全相同的呼叫返回6小时的差异。

Below is the controller that is returning the proper GMT date and below that is the one that is 6 hours off. 下面是返回正确GMT日期的控制器,下面是6小时关闭的控制器。 Hopefully this is just an oversight on my part somewhere. 希望这只是我某处的疏忽。 Thanks in advance for any help. 在此先感谢您的帮助。

    public function update_current_user($session_id){
    $this->load->helper('date');
    $this->load->helper('url');

    $currentURL = base_url().uri_string();

    $updateData = array(
            'id'=>$session_id   ,
            'last_updated'=>gmdate("Y-m-d H:i:s",time()),   
            'current_url'=> $currentURL
        );
    $this->db->where('id',$session_id);
    $this->db->update('current_visitors', $updateData);
    return true;
}

Below is the one where it is returning 6 hours off. 以下是退休6小时的地方。

    public function initiate_chat($id,$name)
{
    $this->load->helper('date');
    $this->load->helper('url');

    $currentURL = base_url().uri_string();

    $updateData = array(
            'id'=>$id   ,
            'last_updated'=>gmdate("Y-m-d H:i:s",time()),
            'chat_requested_time'=>gmdate("Y-m-d H:i:s",time()),    
            'requested_chat'=>1,    
            'name'=>$name,
        );
    $this->db->where('id',$id);
    $this->db->update('current_visitors', $updateData);
    return $name;           
}   

Set, default timezone date_default_timezone_set('GMT'); 设置,默认时区date_default_timezone_set('GMT'); to one value then, you will get same value. 如果是一个值,你将获得相同的价值。 Or use it where you are going to use date/time functions. 或者在您要使用日期/时间功能的地方使用它。 For eg: 例如:

date_default_timezone_set('GMT');
$curTime = gmdate('H:i:s',time());

It will return the current time with timezone set to GMT. 它将返回当前时间,时区设置为GMT。

It's a problem related with Codeigniter and PHP 5. 这是与Codeigniter和PHP 5相关的问题。

You should set a default timezone in your php.ini file or you can use the solution provided by Phil Sturgeon, otherwise you will run into this problem everytime you'll make use of date/time functions 您应该在php.ini文件中设置默认时区,或者您可以使用Phil Sturgeon提供的解决方案,否则每次使用日期/时间函数时都会遇到此问题

http://philsturgeon.co.uk/blog/2009/12/CodeIgniter-on-PHP-5.3 http://philsturgeon.co.uk/blog/2009/12/CodeIgniter-on-PHP-5.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM