diff --git a/application/controllers/Qso.php b/application/controllers/Qso.php index a78840bc..d92f2b51 100755 --- a/application/controllers/Qso.php +++ b/application/controllers/Qso.php @@ -40,6 +40,13 @@ class QSO extends CI_Controller { $data['bands'] = $this->bands->get_user_bands_for_qso_entry(); $data['user_default_band'] = $this->session->userdata('user_default_band'); $data['sat_active'] = array_search("SAT", $this->bands->get_user_bands(), true); + + // Set user's preferred date format + if($this->session->userdata('user_date_format')) { + $data['user_date_format'] = $this->session->userdata('user_date_format'); + } else { + $data['user_date_format'] = $this->config->item('qso_date_format'); + } $this->load->library('form_validation'); 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 diff --git a/application/views/qso/index.php b/application/views/qso/index.php index 5993a1a3..e7ad97f5 100755 --- a/application/views/qso/index.php +++ b/application/views/qso/index.php @@ -69,8 +69,8 @@ required pattern="[0-3][0-9]-[0-1][0-9]-[0-9]{4}"> + echo date($user_date_format); + } ?>" required>