From d78a82c14cfa084192dd216705cc9a7e2414f0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Thu, 20 Feb 2025 22:54:12 +0100 Subject: [PATCH] sync timezone for mysql session --- src/Database/Mysql/Connection.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Database/Mysql/Connection.php b/src/Database/Mysql/Connection.php index 4eb622f..6ebf075 100644 --- a/src/Database/Mysql/Connection.php +++ b/src/Database/Mysql/Connection.php @@ -4,6 +4,8 @@ use SokoWeb\Interfaces\Database\IConnection; use SokoWeb\Interfaces\Database\IResultSet; use SokoWeb\Interfaces\Database\IStatement; use mysqli; +use DateTime; +use DateTimeZone; class Connection implements IConnection { @@ -109,5 +111,16 @@ class Connection implements IConnection mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $this->connection = new mysqli($this->host, $this->user, $this->password, $this->db, $this->port, $this->socket); $this->connection->set_charset('utf8mb4'); + $this->connection->query('SET time_zone = \'' . $this->getTimeZone() . '\''); + } + + private function getTimeZone(): string { + $tz = new DateTimeZone(date_default_timezone_get()); + $offset = $tz->getOffset(new DateTime('now', new DateTimeZone('UTC'))); + + $hours = intdiv($offset, 3600); + $minutes = abs(($offset % 3600) / 60); + + return sprintf("%+03d:%02d", $hours, $minutes); } }