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()); }