Codeigniter timezone mysql settings

后端 未结 5 957
花落未央
花落未央 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条回答
  •  萌比男神i
    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).

提交回复
热议问题