Make PHP use the systems timezone setting

The timezone in the PHP INI file could differ from the timezone set on the system itself and result incorrect time display on webpages and wherever date functions are used.

Simple fix to read the systems timezone and make PHP use the correct one.
This commit is contained in:
jaredb7
2023-04-02 00:27:08 +10:00
parent b51b6df1af
commit e695852d19
2 changed files with 41 additions and 1 deletions
+21 -1
View File
@@ -1,4 +1,24 @@
<?php
<?php
$sys_timezone = "";
// If we can get the timezome from the systems timezone file ust that
if (file_exists('/etc/timezone')) {
$tz_data = file_get_contents('/etc/timezone');
if ($tz_data !== false) {
$sys_timezone = trim($tz_data);
}
} else {
// Else get timezone from the timedatectl command
$tz_data = shell_exec('timedatectl show');
$tz_data_array = parse_ini_string($tz_data);
if (is_array($tz_data_array) && array_key_exists('Timezone', $tz_data_array)) {
$sys_timezone = $tz_data_array['Timezone'];
}
}
//Finally if we have a valod timezone, set it as the one PHP uses
if ($sys_timezone !== "") {
date_default_timezone_set($sys_timezone);
}
session_start();
$user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd");
$user = trim($user);