Skip to content

Commit

Permalink
When MySQL server uses a different TZ than PHP, change the MySQL TZ t…
Browse files Browse the repository at this point in the history
…o match

Fixes #288
  • Loading branch information
gboudreau committed Jan 14, 2022
1 parent c3bbb3c commit 01a4cb7
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions includes/DB.php
Expand Up @@ -62,6 +62,18 @@ public static function connect($retry_until_successful=FALSE, $throw_exception_o
if (self::$options->name != 'mysql') {
DB::migrate();
}

$now = DB::getFirstValue("SELECT NOW()");
$diff = time() - strtotime($now);
if (abs($diff) > 20*60) {
$symbol = $diff < 0 ? '-' : '+';
$diff_minutes = round(abs($diff)/60);
$diff_hours = floor($diff_minutes/60);
$diff_minutes -= $diff_hours*60;
$mysql_tz = sprintf("%s%02d:%02d", $symbol, $diff_hours, $diff_minutes);
Log::info("Adjusting MySQL Timezone: $diff secs difference between MySQL and PHP => Changing MySQL TZ to '$mysql_tz'");
DB::execute("SET time_zone = :tz", ['tz' => $mysql_tz]);
}
}

return self::$handle;
Expand Down

0 comments on commit 01a4cb7

Please sign in to comment.