How to set time zone in codeigniter?

后端 未结 11 1728
醉话见心
醉话见心 2020-11-30 00:02

I am working in a php project using codeigniter. Please advise me what is the global way to set time zone for php and mysql . In which file I can set this. I want to set it

相关标签:
11条回答
  • 2020-11-30 00:15

    Put it in config/config.php, It will work for whole application or index.php of codeigniter.

    0 讨论(0)
  • 2020-11-30 00:18

    you can try this:

    date_default_timezone_set('Asia/Kolkata');
    

    In application/config.php OR application/autoload.php

    There is look like this:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    date_default_timezone_set('Asia/Kolkata');
    

    It's working fine for me, i think this is the best way to define DEFAULT TIMEZONE in codeignitor.

    0 讨论(0)
  • 2020-11-30 00:20

    Placing this date_default_timezone_set('Asia/Kolkata'); on config.php above base url also works

    PHP List of Supported Time Zones

    application/config/config.php

    <?php
    
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    date_default_timezone_set('Asia/Kolkata');
    

    Another way I have found use full is if you wish to set a time zone for each user

    Create a MY_Controller.php

    create a column in your user table you can name it timezone or any thing you want to. So that way when user selects his time zone it can can be set to his timezone when login.

    application/core/MY_Controller.php

    <?php
    
    class MY_Controller extends CI_Controller {
    
        public function __construct() {
            parent::__construct();
            $this->set_timezone();
        }
    
        public function set_timezone() {
            if ($this->session->userdata('user_id')) {
                $this->db->select('timezone');
                $this->db->from($this->db->dbprefix . 'user');
                $this->db->where('user_id', $this->session->userdata('user_id'));
                $query = $this->db->get();
                if ($query->num_rows() > 0) {
                    date_default_timezone_set($query->row()->timezone);
                } else {
                    return false;
                }
            }
        }
    }
    

    Also to get the list of time zones in php

     $timezones =  DateTimeZone::listIdentifiers(DateTimeZone::ALL);
    
     foreach ($timezones as $timezone) 
     {
        echo $timezone;
        echo "</br>";
     }
    
    0 讨论(0)
  • 2020-11-30 00:22

    As describe here

    1. Open your php.ini file (look for it)

    2. Add the following line of code on the top of the file:

      date.timezone = "US/Central"

    3. Verify the changes by going to phpinfo.php

    0 讨论(0)
  • 2020-11-30 00:31

    Add this line to autoload.php in the application folder:

    $autoload['time_zone'] = date_default_timezone_set('Asia/Kolkata');
    
    0 讨论(0)
提交回复
热议问题