Fix date parsing in create_qso() to handle user date format preferences correctly

Co-authored-by: magicbug <84308+magicbug@users.noreply.github.com>
这个提交包含在:
copilot-swe-agent[bot] 2025-07-30 21:39:02 +00:00
父节点 db239bf0a3
当前提交 4edc6560cd

查看文件

@ -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