Skip to content

Commit 996fb5b

Browse files
committed
Set the DB timezone on each connection to match the APP_TIMEZONE value
1 parent 8c6327f commit 996fb5b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

app/Helpers/Time.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Pterodactyl\Helpers;
4+
5+
use Carbon\CarbonImmutable;
6+
7+
final class Time
8+
{
9+
/**
10+
* Gets the time offset from the provided timezone relative to UTC as a number. This
11+
* is used in the database configuration since we can't always rely on there being support
12+
* for named timezones in MySQL.
13+
*
14+
* Returns the timezone as a string like +08:00 or -05:00 depending on the app timezone.
15+
*
16+
* @param string $timezone
17+
* @return string
18+
*/
19+
public static function getMySQLTimezoneOffset(string $timezone): string
20+
{
21+
$offset = round(CarbonImmutable::now($timezone)->getTimezone()->getOffset(CarbonImmutable::now('UTC')) / 3600);
22+
23+
return sprintf('%s%s:00', $offset > 0 ? '+' : '-', str_pad(abs($offset), 2, '0', STR_PAD_LEFT));
24+
}
25+
}

config/database.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Pterodactyl\Helpers\Time;
4+
35
return [
46
/*
57
|--------------------------------------------------------------------------
@@ -43,6 +45,7 @@
4345
'collation' => 'utf8mb4_unicode_ci',
4446
'prefix' => env('DB_PREFIX', ''),
4547
'strict' => env('DB_STRICT_MODE', false),
48+
'timezone' => env('DB_TIMEZONE', Time::getMySQLTimezoneOffset(env('APP_TIMEZONE')))
4649
],
4750

4851
/*
@@ -65,6 +68,7 @@
6568
'collation' => 'utf8mb4_unicode_ci',
6669
'prefix' => '',
6770
'strict' => false,
71+
'timezone' => env('DB_TIMEZONE', Time::getMySQLTimezoneOffset(env('APP_TIMEZONE')))
6872
],
6973
],
7074

0 commit comments

Comments
 (0)