Ensure that TZ setting can work properly in Docker.

This commit is contained in:
Alan Xiong 2024-04-15 05:15:19 +08:00
parent a51b59aa1a
commit 80cf39987a
2 changed files with 22 additions and 7 deletions

View file

@ -12,6 +12,10 @@ RUN apt-get update && apt-get install -y \
&& docker-php-ext-install -j$(nproc) gd pdo pdo_mysql pdo_pgsql pgsql \ && docker-php-ext-install -j$(nproc) gd pdo pdo_mysql pdo_pgsql pgsql \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# Set the timezone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Prepare files and folders # Prepare files and folders
RUN mkdir -p /speedtest/ RUN mkdir -p /speedtest/

View file

@ -3,7 +3,15 @@
require_once 'idObfuscation.php'; require_once 'idObfuscation.php';
define('TELEMETRY_SETTINGS_FILE', 'telemetry_settings.php'); define('TELEMETRY_SETTINGS_FILE', 'telemetry_settings.php');
$tz = getenv('TZ');
if ($tz !== false) {
// If the environment variable TZ exists, set the default timezone for PHP to that value
date_default_timezone_set($tz);
} else {
// If the environment variable TZ does not exist, set a default timezone or handle the error
// Set it to Asia/Shanghai
date_default_timezone_set('Asia/Shanghai');
}
/** /**
* @return PDO|false * @return PDO|false
*/ */
@ -184,19 +192,22 @@ function insertSpeedtestUser($ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping,
} }
try { try {
$currentTimestamp = new DateTime('now');
$formattedTimestamp = $currentTimestamp->format('Y-m-d H:i:s');
$stmt = $pdo->prepare( $stmt = $pdo->prepare(
'INSERT INTO speedtest_users 'INSERT INTO speedtest_users
(ip,ispinfo,extra,ua,lang,dl,ul,ping,jitter,log) (ip,ispinfo,extra,ua,lang,dl,ul,ping,jitter,log,timestamp)
VALUES (?,?,?,?,?,?,?,?,?,?)' VALUES (?,?,?,?,?,?,?,?,?,?,?)'
); );
$stmt->execute([ $stmt->execute([
$ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping, $jitter, $log $ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping, $jitter, $log, $formattedTimestamp // 在execute的参数中添加$formattedTimestamp
]); ]);
$id = $pdo->lastInsertId(); $id = $pdo->lastInsertId();
} catch (Exception $e) { } catch (Exception $e) {
if($returnExceptionOnError){ if($returnExceptionOnError){
return $e; return $e;
} }
return false; return false;
} }