Codeigniter timezone mysql settings

后端 未结 5 951
花落未央
花落未央 2020-12-31 19:46

Just realised WHY my site is now showing all datetime variables as -1 hr... I\'m using Codeigniter for the first time! (Never had this problem before)

So, I have inc

相关标签:
5条回答
  • 2020-12-31 19:51

    Add these line in your config file and then check, it is working for me

    $config['time_reference'] = 'gmt';# Default should be GMT
    date_default_timezone_set('UTC');# Add this line after creating timezone to GMT for reflecting
    
    0 讨论(0)
  • 2020-12-31 19:54

    am also face this problem. i tried this way..

    $this->db->query("SET LOCAL time_zone='Asia/Kolkata'");
    $query="SELECT * FROM offers where NOW() between offfer_from and offer_to";
    $res=$this->db->query($query);
    

    it will work fine in my project and i'm not using anything new default model.. if you want global solution means you can use auto-load some model.

    0 讨论(0)
  • 2020-12-31 20:02

    In config/autoload.php, set a model to load on each page load. then call $this->db->query("SET time_zone='+0:00'"); in that model constructor.

    config/autoload.php

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

    In application/models, create a new model file with name of "default_model.php" and add below code.

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

    On each page load, this constructor will be called and mysql timezone will be set to +0:00.

    0 讨论(0)
  • 2020-12-31 20:03

    I like the solution proposed by Kumar however, I didn't need to set this globally, only when showing dates stored in UTC time in my db.

    In my api model I have the following function.

      public function setDBTimeOffset(){
        $dt = new DateTime();
        $offset = $dt->format("P");
        $result = $this->db->query("SET time_zone='$offset';");
        return $result;
      } # END FUNCTION setDBTimeOffset
    

    From my controller, I call the following first.

      $this->api->setDBTimeOffset();
    

    Followed by the call to the function that queries the db. For example, I am fetching some user history.

      $data["viewData"]["allHistory"] = $this->api->getUserHistory("ALL", 0, 10000);
    

    When I display the output, the dates are in my timezone. I hope this helps someone, somewhere in time (couldn't resist).

    0 讨论(0)
  • 2020-12-31 20:18

    You can set it in the index.php file in your project folder on top and after <?php

    <?php
    date_default_timezone_set('Asia/Bangkok');
    
    0 讨论(0)
提交回复
热议问题