From 799df8d6c868e0ce560b344d4c0d7764d814baef Mon Sep 17 00:00:00 2001 From: Agent Date: Sun, 7 Jun 2026 13:14:07 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20MySQL=20timezone=20=E2=80=94=20sync=20ses?= =?UTF-8?q?sion=20timezone=20with=20app=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/App.php | 4 +++- src/Core/Database.php | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Core/App.php b/src/Core/App.php index c101ccc..9d28dc2 100755 --- a/src/Core/App.php +++ b/src/Core/App.php @@ -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']); diff --git a/src/Core/Database.php b/src/Core/Database.php index 4eaf910..685b2f8 100755 --- a/src/Core/Database.php +++ b/src/Core/Database.php @@ -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()); } -- 2.43.0