Skip to content

A live bird collage card for your Home Assistant dashboard.

Make PHP use the systems timezone setting

jaredb7e695852d197d
diff --git a/homepage/index.php b/homepage/index.php
index 63e86e99..7d058300 100644
--- a/homepage/index.php
+++ b/homepage/index.php
@@ -1,4 +1,24 @@
<?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);
+}
+
if (file_exists('./scripts/thisrun.txt')) {
$config = parse_ini_file('./scripts/thisrun.txt');
} elseif (file_exists('./scripts/firstrun.ini')) {
diff --git a/homepage/views.php b/homepage/views.php
index 96db5f7b..2416116c 100644
--- a/homepage/views.php
+++ b/homepage/views.php
@@ -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);