简体   繁体   中英

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. The controllers are returning a 6 hour difference with the exact same calls.

Below is the controller that is returning the proper GMT date and below that is the one that is 6 hours off. 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.

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

It's a problem related with Codeigniter and 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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