Skip to content

Added support for Mysql timezone on connection #973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions MysqliDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ class MysqliDb
*/
protected $_transaction_in_progress = false;

protected $timezone;

/**
* @param string $host
* @param string $username
Expand All @@ -261,7 +263,7 @@ class MysqliDb
* @param string $charset
* @param string $socket
*/
public function __construct($host = null, $username = null, $password = null, $db = null, $port = null, $charset = 'utf8', $socket = null)
public function __construct($host = null, $username = null, $password = null, $db = null, $port = null, $charset = 'utf8', $timezone = null, $socket = null)
{
$isSubQuery = false;

Expand Down Expand Up @@ -291,6 +293,10 @@ public function __construct($host = null, $username = null, $password = null, $d
$this->setPrefix($prefix);
}

if (isset($timezone)) {
$this->timezone = $timezone;
}

self::$_instance = $this;
}

Expand Down Expand Up @@ -329,6 +335,11 @@ public function connect($connectionName = 'default')
if (!empty($charset)) {
$mysqli->set_charset($charset);
}

if ($this->timezone) {
$mysqli->query("SET time_zone = '" . $this->timezone . "'");
}

$this->_mysqli[$connectionName] = $mysqli;
}

Expand Down Expand Up @@ -551,7 +562,7 @@ private function queryUnprepared($query)
* @return string Contains the returned rows from the query.
*/
public function rawAddPrefix($query){
$query = str_replace(PHP_EOL, null, $query);
$query = str_replace(PHP_EOL, ' ', $query);
$query = preg_replace('/\s+/', ' ', $query);
preg_match_all("/(from|into|update|join) [\\'\\´]?([a-zA-Z0-9_-]+)[\\'\\´]?/i", $query, $matches);
list($from_table, $from, $table) = $matches;
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ $db = new MysqliDb (Array (
'db'=> 'databaseName',
'port' => 3306,
'prefix' => 'my_',
'charset' => 'utf8'));
'charset' => 'utf8',
'timezone' => '-5:00'));
```
table prefix, port and database charset params are optional.
If no charset should be set charset, set it to null
Expand Down