LuminaWebsite/includes/db.php
Maaxxs 084e96417d Use German time for PHP and the database session
The bot turns raid and event times into timestamps as German time, but
the website formatted them in the hoster's default timezone (UTC). A raid
set to 9:00 therefore showed as 7:00 during summer time. The edit form
was prefilled with that wrong time, so saving a raid again would really
move it.

PHP now uses Europe/Berlin, and each database connection sets the same
offset, so NOW() and CURRENT_TIMESTAMP match. The offset follows
summer/winter time automatically.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 11:05:27 +02:00

36 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
function db(): PDO
{
static $pdo = null;
if ($pdo === null) {
$configPath = __DIR__ . '/../config.php';
if (!file_exists($configPath)) {
http_response_code(500);
exit('Konfigurationsdatei fehlt. Bitte config.example.php zu config.php kopieren und ausfüllen.');
}
$config = require $configPath;
$db = $config['db'];
$dsn = "mysql:host={$db['host']};dbname={$db['name']};charset={$db['charset']}";
try {
$pdo = new PDO($dsn, $db['user'], $db['pass'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
} catch (PDOException $e) {
http_response_code(500);
exit('Datenbankverbindung fehlgeschlagen. Bitte config.php prüfen.');
}
// NOW() und CURRENT_TIMESTAMP in derselben Zeitzone wie PHP (deutsche Zeit inkl.
// Sommerzeit). Als Versatz, weil viele Hoster die Zeitzonen-Namen in MySQL nicht geladen haben.
$pdo->exec("SET time_zone = '" . (new DateTimeImmutable('now', new DateTimeZone(date_default_timezone_get())))->format('P') . "'");
}
return $pdo;
}