7

How can I change MYSQL TIMEZONE to GMT format like this function in PHP: /SET GMT TIMEZONE/ date_default_timezone_set('Etc/GMT');

My DB class is here:

class DB {
    private static $instance;
    private $MySQLi;

    private function __construct(array $dbOptions){

        $this->MySQLi = @ new mysqli(   $dbOptions['db_host'],
                                        $dbOptions['db_user'],
                                        $dbOptions['db_pass'],
                                        $dbOptions['db_name'] );

        if (mysqli_connect_errno()) {
            throw new Exception('Database error.');
        }

        $this->MySQLi->set_charset("utf8");
    }

    public static function init(array $dbOptions){
        if(self::$instance instanceof self){
            return false;
        }

        self::$instance = new self($dbOptions);
    }

    public static function getMySQLiObject(){
        return self::$instance->MySQLi;
    }

    public static function query($q){
        return self::$instance->MySQLi->query($q);
    }

    public static function prepare($q){
        return self::$instance->MySQLi->prepare($q);
    }

    public static function esc($str){
        return self::$instance->MySQLi->real_escape_string(htmlspecialchars($str));
    }

}

And my queries like this in other files:

DB::query('UPDATE `calendar_data` SET `data` = "'.DB::esc(json_encode($array)).'", `upcoming_time` = "'.date('Y-m-d H:i:s', $upcoming).'", `time_now` = NOW() WHERE `id` = "1"');

Where should I insert my query to the class for run it once?

Thanks!

$this->MySQLi->set_charset("utf8");
$this->MySQLi->query("SET timezone = 'GMT'");
  • Doesn't work. If I use NOW(), server time was inserted (GMT-4). Where is a problem???
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60

4 Answers4

11

Try the following:

$this->MySQLi->query("SET time_zone = '+0:00'");

Using named timezones will only work if the time zone information tables in the MySQL database have been created and populated.

Lorenzo Lapucci
  • 129
  • 4
  • 12
Mike
  • 1,791
  • 13
  • 13
6

After you write this:

$this->MySQLi->set_charset("utf8");

Also write this:

$this->MySQLi->query("SET timezone = 'GMT'");

This timezone setting will apply to all future queries sent on that connection.

Also, ensure you've set up the zone tables in the mysql database.

http://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • Doesn't work. If I use NOW(), server time was inserted (GMT-4). Where is a problem??? – XTRUST.ORG Sep 06 '12 at 09:52
  • 1
    Did you run `SET timezone` again before you `SELECT`ed the date back out to check what was inserted? If you connect with the `mysql` CLI and run a query it's going to use the server time zone again. – Dan Grossman Sep 06 '12 at 09:54
  • set timezone was placed to the script before my INSERT query. I i'm tried to run this query: DB::query('UPDATE `users_calendar_data` SET `data` = "'.DB::esc(json_encode($array)).'", `upcoming_time` = "'.date('Y-m-d H:i:s', $upcoming).'", `time_now` = NOW() WHERE `id` = "1"'); But my time_now value is equal to the server time (not GMT). – XTRUST.ORG Sep 06 '12 at 10:33
  • If you think it's in the wrong time zone, that means you must be `SELECT`ing it somewhere look at what you `UPDATE`d. Are you setting the time zone on THAT connection? Also, did you populate the MySQL tzinfo database? You can't do any of this without that. http://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html – Dan Grossman Sep 06 '12 at 10:55
  • 3
    According to the MySQL documentation the variable is named "time_zone" not "timezone". – Robert Nov 14 '13 at 14:45
4

From manual;

Per-connection time zones. Each client that connects has its own time zone setting, given by the session time_zone variable. Initially, the session variable takes its value from the global time_zone variable, but the client can change its own time zone with this statement:

mysql> SET time_zone = timezone;

So must be like this;

$this->MySQLi->query("SET time_zone = 'GMT'");

http://dev.mysql.com/doc/refman/5.5/en//time-zone-support.html
http://dev.mysql.com/doc/refman/5.5/en//server-system-variables.html#sysvar_time_zone

Kerem
  • 11,377
  • 5
  • 59
  • 58
3

MySQL has extensive timezone support. You can set it on server level, on a per connection basis and even per query. I think executing 'SET time_zone = xxx' in __construct is enough for you.

Sherlock
  • 7,525
  • 6
  • 38
  • 79