From 656f4306325d7944280ce111d95aeaeacab8d3cd Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 5 Jun 2016 21:28:56 -0400 Subject: Changed DateParser to use only user format and ISO-8601 --- app/Core/DateParser.php | 66 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) (limited to 'app/Core') diff --git a/app/Core/DateParser.php b/app/Core/DateParser.php index 835eb3e3..bdab7998 100644 --- a/app/Core/DateParser.php +++ b/app/Core/DateParser.php @@ -15,6 +15,28 @@ class DateParser extends Base const DATE_FORMAT = 'm/d/Y'; const DATE_TIME_FORMAT = 'm/d/Y H:i'; + /** + * Get date format from settings + * + * @access public + * @return string + */ + public function getUserDateFormat() + { + return $this->configModel->get('application_date_format', DateParser::DATE_FORMAT); + } + + /** + * Get date time format from settings + * + * @access public + * @return string + */ + public function getUserDateTimeFormat() + { + return $this->configModel->get('application_datetime_format', DateParser::DATE_TIME_FORMAT); + } + /** * List of time formats * @@ -38,19 +60,29 @@ class DateParser extends Base */ public function getDateFormats($iso = false) { - $iso_formats = array( + $formats = array( + $this->getUserDateFormat(), + ); + + $isoFormats = array( 'Y-m-d', 'Y_m_d', ); - $user_formats = array( + $userFormats = array( 'm/d/Y', 'd/m/Y', 'Y/m/d', 'd.m.Y', ); - return $iso ? array_merge($iso_formats, $user_formats) : $user_formats; + if ($iso) { + $formats = array_merge($formats, $isoFormats, $userFormats); + } else { + $formats = array_merge($formats, $userFormats); + } + + return array_unique($formats); } /** @@ -62,7 +94,9 @@ class DateParser extends Base */ public function getDateTimeFormats($iso = false) { - $formats = array(); + $formats = array( + $this->getUserDateTimeFormat(), + ); foreach ($this->getDateFormats($iso) as $date) { foreach ($this->getTimeFormats() as $time) { @@ -70,7 +104,7 @@ class DateParser extends Base } } - return $formats; + return array_unique($formats); } /** @@ -97,12 +131,30 @@ class DateParser extends Base $values = array(); foreach ($formats as $format) { - $values[$format] = date($format); + $values[$format] = date($format).' ('.$format.')'; } return $values; } + /** + * Get formats for date parsing + * + * @access public + * @return array + */ + public function getParserFormats() + { + return array( + $this->getUserDateFormat(), + 'Y-m-d', + 'Y_m_d', + $this->getUserDateTimeFormat(), + 'Y-m-d H:i', + 'Y_m_d H:i', + ); + } + /** * Parse a date and return a unix timestamp, try different date formats * @@ -116,7 +168,7 @@ class DateParser extends Base return (int) $value; } - foreach ($this->getAllDateFormats(true) as $format) { + foreach ($this->getParserFormats() as $format) { $timestamp = $this->getValidDate($value, $format); if ($timestamp !== 0) { -- cgit v1.2.3