diff options
Diffstat (limited to 'app/Core/DateParser.php')
-rw-r--r-- | app/Core/DateParser.php | 218 |
1 files changed, 125 insertions, 93 deletions
diff --git a/app/Core/DateParser.php b/app/Core/DateParser.php index 6577af0f..835eb3e3 100644 --- a/app/Core/DateParser.php +++ b/app/Core/DateParser.php @@ -12,68 +12,95 @@ use DateTime; */ class DateParser extends Base { + const DATE_FORMAT = 'm/d/Y'; + const DATE_TIME_FORMAT = 'm/d/Y H:i'; + /** - * Return true if the date is within the date range + * List of time formats * * @access public - * @param DateTime $date - * @param DateTime $start - * @param DateTime $end - * @return boolean + * @return string[] */ - public function withinDateRange(DateTime $date, DateTime $start, DateTime $end) + public function getTimeFormats() { - return $date >= $start && $date <= $end; + return array( + 'H:i', + 'g:i a', + ); } /** - * Get the total number of hours between 2 datetime objects - * Minutes are rounded to the nearest quarter + * List of date formats * * @access public - * @param DateTime $d1 - * @param DateTime $d2 - * @return float + * @param boolean $iso + * @return string[] */ - public function getHours(DateTime $d1, DateTime $d2) + public function getDateFormats($iso = false) { - $seconds = $this->getRoundedSeconds(abs($d1->getTimestamp() - $d2->getTimestamp())); - return round($seconds / 3600, 2); + $iso_formats = array( + 'Y-m-d', + 'Y_m_d', + ); + + $user_formats = array( + 'm/d/Y', + 'd/m/Y', + 'Y/m/d', + 'd.m.Y', + ); + + return $iso ? array_merge($iso_formats, $user_formats) : $user_formats; } /** - * Round the timestamp to the nearest quarter + * List of datetime formats * * @access public - * @param integer $seconds Timestamp - * @return integer + * @param boolean $iso + * @return string[] */ - public function getRoundedSeconds($seconds) + public function getDateTimeFormats($iso = false) { - return (int) round($seconds / (15 * 60)) * (15 * 60); + $formats = array(); + + foreach ($this->getDateFormats($iso) as $date) { + foreach ($this->getTimeFormats() as $time) { + $formats[] = $date.' '.$time; + } + } + + return $formats; } /** - * Return a timestamp if the given date format is correct otherwise return 0 + * List of all date formats * * @access public - * @param string $value Date to parse - * @param string $format Date format - * @return integer + * @param boolean $iso + * @return string[] */ - public function getValidDate($value, $format) + public function getAllDateFormats($iso = false) { - $date = DateTime::createFromFormat($format, $value); + return array_merge($this->getDateFormats($iso), $this->getDateTimeFormats($iso)); + } - if ($date !== false) { - $errors = DateTime::getLastErrors(); - if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) { - $timestamp = $date->getTimestamp(); - return $timestamp > 0 ? $timestamp : 0; - } + /** + * Get available formats (visible in settings) + * + * @access public + * @param array $formats + * @return array + */ + public function getAvailableFormats(array $formats) + { + $values = array(); + + foreach ($formats as $format) { + $values[$format] = date($format); } - return 0; + return $values; } /** @@ -85,7 +112,11 @@ class DateParser extends Base */ public function getTimestamp($value) { - foreach ($this->getAllFormats() as $format) { + if (ctype_digit($value)) { + return (int) $value; + } + + foreach ($this->getAllDateFormats(true) as $format) { $timestamp = $this->getValidDate($value, $format); if ($timestamp !== 0) { @@ -97,104 +128,103 @@ class DateParser extends Base } /** - * Get ISO8601 date from user input + * Return a timestamp if the given date format is correct otherwise return 0 * - * @access public - * @param string $value Date to parse - * @return string + * @access private + * @param string $value Date to parse + * @param string $format Date format + * @return integer */ - public function getIsoDate($value) + private function getValidDate($value, $format) { - return date('Y-m-d', ctype_digit($value) ? $value : $this->getTimestamp($value)); + $date = DateTime::createFromFormat($format, $value); + + if ($date !== false) { + $errors = DateTime::getLastErrors(); + if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) { + $timestamp = $date->getTimestamp(); + return $timestamp > 0 ? $timestamp : 0; + } + } + + return 0; } /** - * Get all combinations of date/time formats + * Return true if the date is within the date range * * @access public - * @return string[] + * @param DateTime $date + * @param DateTime $start + * @param DateTime $end + * @return boolean */ - public function getAllFormats() + public function withinDateRange(DateTime $date, DateTime $start, DateTime $end) { - $formats = array(); - - foreach ($this->getDateFormats() as $date) { - foreach ($this->getTimeFormats() as $time) { - $formats[] = $date.' '.$time; - } - } - - return array_merge($formats, $this->getDateFormats()); + return $date >= $start && $date <= $end; } /** - * Return the list of supported date formats (for the parser) + * Get the total number of hours between 2 datetime objects + * Minutes are rounded to the nearest quarter * * @access public - * @return string[] + * @param DateTime $d1 + * @param DateTime $d2 + * @return float */ - public function getDateFormats() + public function getHours(DateTime $d1, DateTime $d2) { - return array( - $this->config->get('application_date_format', 'm/d/Y'), - 'Y-m-d', - 'Y_m_d', - ); + $seconds = $this->getRoundedSeconds(abs($d1->getTimestamp() - $d2->getTimestamp())); + return round($seconds / 3600, 2); } /** - * Return the list of supported time formats (for the parser) + * Round the timestamp to the nearest quarter * * @access public - * @return string[] + * @param integer $seconds Timestamp + * @return integer */ - public function getTimeFormats() + public function getRoundedSeconds($seconds) { - return array( - 'H:i', - 'g:i A', - 'g:iA', - ); + return (int) round($seconds / (15 * 60)) * (15 * 60); } /** - * Return the list of available date formats (for the config page) + * Get ISO-8601 date from user input * * @access public - * @return array + * @param string $value Date to parse + * @return string */ - public function getAvailableFormats() + public function getIsoDate($value) { - return array( - 'm/d/Y' => date('m/d/Y'), - 'd/m/Y' => date('d/m/Y'), - 'Y/m/d' => date('Y/m/d'), - 'd.m.Y' => date('d.m.Y'), - ); + return date('Y-m-d', $this->getTimestamp($value)); } /** - * Remove the time from a timestamp + * Get a timestamp from an ISO date format * * @access public - * @param integer $timestamp Timestamp + * @param string $value * @return integer */ - public function removeTimeFromTimestamp($timestamp) + public function getTimestampFromIsoFormat($value) { - return mktime(0, 0, 0, date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp)); + return $this->removeTimeFromTimestamp(ctype_digit($value) ? $value : strtotime($value)); } /** - * Get a timetstamp from an ISO date format + * Remove the time from a timestamp * * @access public - * @param string $date + * @param integer $timestamp * @return integer */ - public function getTimestampFromIsoFormat($date) + public function removeTimeFromTimestamp($timestamp) { - return $this->removeTimeFromTimestamp(ctype_digit($date) ? $date : strtotime($date)); + return mktime(0, 0, 0, date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp)); } /** @@ -204,13 +234,10 @@ class DateParser extends Base * @param array $values Database values * @param string[] $fields Date fields * @param string $format Date format + * @return array */ - public function format(array &$values, array $fields, $format = '') + public function format(array $values, array $fields, $format) { - if ($format === '') { - $format = $this->config->get('application_date_format'); - } - foreach ($fields as $field) { if (! empty($values[$field])) { $values[$field] = date($format, $values[$field]); @@ -218,23 +245,28 @@ class DateParser extends Base $values[$field] = ''; } } + + return $values; } /** - * Convert date (form input data) + * Convert date to timestamp * * @access public * @param array $values Database values * @param string[] $fields Date fields * @param boolean $keep_time Keep time or not + * @return array */ - public function convert(array &$values, array $fields, $keep_time = false) + public function convert(array $values, array $fields, $keep_time = false) { foreach ($fields as $field) { - if (! empty($values[$field]) && ! is_numeric($values[$field])) { + if (! empty($values[$field])) { $timestamp = $this->getTimestamp($values[$field]); $values[$field] = $keep_time ? $timestamp : $this->removeTimeFromTimestamp($timestamp); } } + + return $values; } } |