Fix MySQL timezone — sync session timezone with app config
This commit is contained in:
@@ -50,7 +50,9 @@ class App
|
||||
|
||||
date_default_timezone_set($this->config['app']['timezone'] ?? 'UTC');
|
||||
|
||||
Database::getInstance($this->config['database']);
|
||||
$dbConfig = $this->config['database'];
|
||||
$dbConfig['timezone'] = $this->config['app']['timezone'] ?? 'UTC';
|
||||
Database::getInstance($dbConfig);
|
||||
|
||||
Session::init($this->config['session']);
|
||||
|
||||
|
||||
@@ -33,6 +33,16 @@ class Database
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
$initCommand => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci',
|
||||
]);
|
||||
|
||||
$tz = $config['timezone'] ?? 'UTC';
|
||||
$offset = (new \DateTimeZone($tz))->getOffset(new \DateTimeImmutable('now', new \DateTimeZone('UTC')));
|
||||
$sign = $offset >= 0 ? '+' : '-';
|
||||
$offset = abs($offset);
|
||||
$hours = str_pad((string) intdiv($offset, 3600), 2, '0', STR_PAD_LEFT);
|
||||
$minutes = str_pad((string) (($offset % 3600) / 60), 2, '0', STR_PAD_LEFT);
|
||||
$tzOffset = sprintf('%s%s:%s', $sign, $hours, $minutes);
|
||||
|
||||
$this->pdo->exec("SET time_zone = '{$tzOffset}'");
|
||||
} catch (PDOException $e) {
|
||||
throw new \RuntimeException('Database connection failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user