From 4edc6560cd386681ad358d58d2206ccdfe3190f6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 30 Jul 2025 21:39:02 +0000 Subject: [PATCH] Fix date parsing in create_qso() to handle user date format preferences correctly Co-authored-by: magicbug <84308+magicbug@users.noreply.github.com> --- application/models/Logbook_model.php | 43 ++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 49a7d9ce..12a70150 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -8,10 +8,11 @@ class Logbook_model extends CI_Model { $callsign = str_replace('Ø', '0', $this->input->post('callsign')); - // Join date+time - $datetime = date("Y-m-d", strtotime($this->input->post('start_date'))) . " " . $this->input->post('start_time'); + // Join date+time - Parse date according to user's format preference + $parsed_date = $this->parse_user_date($this->input->post('start_date')); + $datetime = $parsed_date . " " . $this->input->post('start_time'); if ($this->input->post('end_time') != null) { - $datetime_off = date("Y-m-d", strtotime($this->input->post('start_date'))) . " " . $this->input->post('end_time'); + $datetime_off = $parsed_date . " " . $this->input->post('end_time'); // if time off < time on, and time off is on 00:xx >> add 1 day (concidering start and end are between 23:00 and 00:59) // $_tmp_datetime_off = strtotime($datetime_off); if (($_tmp_datetime_off < strtotime($datetime)) && (substr($this->input->post('end_time'), 0, 2) == "00")) { @@ -4942,6 +4943,42 @@ class Logbook_model extends CI_Model // Step 6: Return Table HTML return $table; } + + /** + * Parse date from user input according to user's preferred date format + * @param string $date_input The date string from user input + * @param string $user_format The user's preferred date format (e.g., 'd/m/Y', 'Y-m-d') + * @return string Returns date in Y-m-d format for database storage, or original input if parsing fails + */ + private function parse_user_date($date_input, $user_format = null) { + if (empty($date_input)) { + return $date_input; + } + + // If no user format provided, try to get it from session or config + if ($user_format === null) { + if ($this->session->userdata('user_date_format')) { + $user_format = $this->session->userdata('user_date_format'); + } else { + $user_format = $this->config->item('qso_date_format'); + } + } + + // Try to parse with the user's format first + $date = DateTime::createFromFormat($user_format, $date_input); + if ($date !== false) { + return $date->format('Y-m-d'); + } + + // Fallback to strtotime for formats it can handle (mostly Y-m-d, m/d/Y, etc.) + $timestamp = strtotime($date_input); + if ($timestamp !== false) { + return date('Y-m-d', $timestamp); + } + + // If all parsing fails, return the original input and let the database handle it + return $date_input; + } } // Function to validate ADIF date format