From 556a3b3805686f3192d3deb65ba369a4e9a3e697 Mon Sep 17 00:00:00 2001 From: Wolfgang Tischer Date: Mon, 22 Sep 2025 09:55:33 +0200 Subject: [PATCH 01/10] Fix timezone handling for form start/end datetime fields - Fix 2-hour offset bug where forms disabled too early (UTC vs local time) - Properly use WordPress timezone settings in FormSettings::convertFormatToTime() - Add timezone conversion for display logic in cforms-options.php - Fix datetime storage with correct timezone handling in lib_options_sub.php - Resolve DateTime namespace conflicts by adding backslash prefix - Tested with MESZ/CET and manual UTC offset configurations Fixes issue where forms with end dates would deactivate at 22:00 instead of 00:00 when WordPress timezone was set to Europe/Berlin (MESZ). --- FormSettings.php | 35 +++++++++++++++++++++++---- cforms-options.php | 21 +++++++++++----- lib_options_sub.php | 58 +++++++++++++++++++++++++++++---------------- 3 files changed, 83 insertions(+), 31 deletions(-) diff --git a/FormSettings.php b/FormSettings.php index 9e4cd30..e6ff3bc 100644 --- a/FormSettings.php +++ b/FormSettings.php @@ -66,12 +66,37 @@ private function convertFormatToTime($formatted_date) if (trim($formatted_date) === '') { return 0; } - $time = str_replace('/', '.', $formatted_date) . sprintf(' %+d', get_option('gmt_offset')); - $time = strtotime($time); - if ($time === false) { - return 0; + + // WordPress-Zeitzone verwenden + $timezone_string = get_option('timezone_string'); + if (empty($timezone_string)) { + // Fallback für manuelle UTC-Offsets + $gmt_offset = get_option('gmt_offset'); + $timezone_string = timezone_name_from_abbr('', $gmt_offset * 3600, 0); + if ($timezone_string === false) { + // Weitere Fallback-Option + $timezone_string = $gmt_offset >= 0 ? '+' . $gmt_offset : $gmt_offset; + $timezone_string = 'Etc/GMT' . $timezone_string; + } + } + + try { + $timezone = new \DateTimeZone($timezone_string); + $dt = \DateTime::createFromFormat('d/m/Y H:i', $formatted_date, $timezone); + if ($dt !== false) { + return $dt->getTimestamp(); + } + } catch (\Exception $e) { + // Fallback zur alten Methode bei Fehlern, aber mit current_time + $time = str_replace('/', '.', $formatted_date); + $time = strtotime($time); + if ($time !== false) { + // Konvertierung von lokaler Zeit zu UTC-Timestamp + return $time - (get_option('gmt_offset') * 3600) + (current_time('timestamp') - time()); + } } - return $time; + + return 0; } public function getStartDateTime() diff --git a/cforms-options.php b/cforms-options.php index ff5d750..d9469dc 100644 --- a/cforms-options.php +++ b/cforms-options.php @@ -616,11 +616,15 @@ getStartDateTime(); ?> - - + setTimezone($tz) : null; +?> + +