Fix MySQL timezone — sync session timezone with app config

This commit is contained in:
2026-06-07 13:14:07 -04:00
parent 5789c77d9a
commit 799df8d6c8
2 changed files with 13 additions and 1 deletions

View File

@@ -50,7 +50,9 @@ class App
date_default_timezone_set($this->config['app']['timezone'] ?? 'UTC'); 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']); Session::init($this->config['session']);

View File

@@ -33,6 +33,16 @@ class Database
PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_EMULATE_PREPARES => false,
$initCommand => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci', $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) { } catch (PDOException $e) {
throw new \RuntimeException('Database connection failed: ' . $e->getMessage()); throw new \RuntimeException('Database connection failed: ' . $e->getMessage());
} }