diff options
Diffstat (limited to 'app/Model/DateParser.php')
-rw-r--r-- | app/Model/DateParser.php | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/app/Model/DateParser.php b/app/Model/DateParser.php index be79a92e..79a8385c 100644 --- a/app/Model/DateParser.php +++ b/app/Model/DateParser.php @@ -85,8 +85,7 @@ class DateParser extends Base */ public function getTimestamp($value) { - foreach ($this->getDateFormats() as $format) { - + foreach ($this->getAllFormats() as $format) { $timestamp = $this->getValidDate($value, $format); if ($timestamp !== 0) { @@ -98,6 +97,25 @@ class DateParser extends Base } /** + * Get all combinations of date/time formats + * + * @access public + * @return []string + */ + public function getAllFormats() + { + $formats = array(); + + foreach ($this->getDateFormats() as $date) { + foreach ($this->getTimeFormats() as $time) { + $formats[] = $date.' '.$time; + } + } + + return array_merge($formats, $this->getDateFormats()); + } + + /** * Return the list of supported date formats (for the parser) * * @access public @@ -113,6 +131,21 @@ class DateParser extends Base } /** + * Return the list of supported time formats (for the parser) + * + * @access public + * @return string[] + */ + public function getTimeFormats() + { + return array( + 'H:i', + 'g:i A', + 'g:iA', + ); + } + + /** * Return the list of available date formats (for the config page) * * @access public @@ -143,7 +176,7 @@ class DateParser extends Base * Get a timetstamp from an ISO date format * * @access public - * @param string $date Date format + * @param string $date * @return integer */ public function getTimestampFromIsoFormat($date) @@ -166,7 +199,6 @@ class DateParser extends Base } foreach ($fields as $field) { - if (! empty($values[$field])) { $values[$field] = date($format, $values[$field]); } @@ -180,15 +212,16 @@ class DateParser extends Base * Convert date (form input data) * * @access public - * @param array $values Database values - * @param string[] $fields Date fields + * @param array $values Database values + * @param string[] $fields Date fields + * @param boolean $keep_time Keep time or not */ - public function convert(array &$values, array $fields) + public function convert(array &$values, array $fields, $keep_time = false) { foreach ($fields as $field) { - if (! empty($values[$field]) && ! is_numeric($values[$field])) { - $values[$field] = $this->removeTimeFromTimestamp($this->getTimestamp($values[$field])); + $timestamp = $this->getTimestamp($values[$field]); + $values[$field] = $keep_time ? $timestamp : $this->removeTimeFromTimestamp($timestamp); } } } |