Formats and parses dates using the SimpleDateFormat pattern.
This pattern is compatible with the I18N and java's SimpleDateFormatter.
Pattern | Description
----------------------------------------------------
d | Day of month 1 to 31, no padding
dd | Day of monath 01 to 31, zero leading
M | Month digit 1 to 12, no padding
MM | Month digit 01 to 12, zero leading
yy | 2 year digit, e.g., 96, 05
yyyy | 4 year digit, e.g., 2005
----------------------------------------------------
Usage example, to format a date
$formatter = new TSimpleDateFormatter("dd/MM/yyy");
echo $formatter->format(time());
To parse the date string into a date timestamp.
$formatter = new TSimpleDateFormatter("d-M-yyy");
echo $formatter->parse("24-6-2005");
/** * Parse the pair of values into the appropriate value type. * @param string value one * @param string second value * @return array appropriate type of the value pair, array($value1, $value2); */ protected function getComparisonValues($value1, $value2) { switch ($this->getDataType()) { case TValidationDataType::Integer: return array(intval($value1), intval($value2)); case TValidationDataType::Float: return array(floatval($value1), floatval($value2)); case TValidationDataType::Date: $dateFormat = $this->getDateFormat(); if ($dateFormat !== '') { $formatter = new TSimpleDateFormatter($dateFormat); return array($formatter->parse($value1), $formatter->parse($value2)); } else { return array(strtotime($value1), strtotime($value2)); } } return array($value1, $value2); }
/** * Gets the date from the text input using TSimpleDateFormatter * @return integer current selected date timestamp */ protected function getTimeStampFromText() { $pattern = $this->getDateFormat(); $pattern = str_replace(array('MMMM', 'MMM'), array('MM', 'MM'), $pattern); $formatter = new TSimpleDateFormatter($pattern); return $formatter->parse($this->getText()); }
public function testMissingYearPattern() { $formatter = new TSimpleDateFormatter("MM/dd"); self::assertEquals(date("Y-10-22"), date('Y-m-d', $formatter->parse("10/22"))); }
/** * Determine if the given value is of a particular type using RegExp. * @param string value to check * @return boolean true if value fits the type expression. */ protected function evaluateDataTypeCheck($value) { if ($value == '') { return true; } switch ($this->getDataType()) { case TValidationDataType::Integer: return preg_match('/^[-+]?[0-9]+$/', trim($value)); case TValidationDataType::Float: return preg_match('/^[-+]?([0-9]*\\.)?[0-9]+([eE][-+]?[0-9]+)?$/', trim($value)); case TValidationDataType::Date: $dateFormat = $this->getDateFormat(); if (strlen($dateFormat)) { $formatter = new TSimpleDateFormatter($dateFormat); return $formatter->isValidDate($value); } else { return strtotime($value) > 0; } } return true; }
/** * Returns the localized month names that depends on the month format pattern. * "MMMM" will return the month names, "MM" or "MMM" return abbr. month names * and "M" return month digits. * @param DateTimeFormatInfo localized date format information. * @return array localized month names. */ protected function getLocalizedMonthNames($info) { $formatter = new TSimpleDateFormatter($this->getDateFormat()); switch ($formatter->getMonthPattern()) { case 'MMM': return $info->getAbbreviatedMonthNames(); case 'MM': $array = array(); for ($i = 1; $i <= 12; $i++) { $array[$i - 1] = $i < 10 ? '0' . $i : $i; } return $array; case 'M': $array = array(); for ($i = 1; $i <= 12; $i++) { $array[$i - 1] = $i; } return $array; default: return $info->getMonthNames(); } }
/** * Determine if the date is within the specified range. * Uses pradoParseDate and strtotime to get the date from string. * @param string date as string to validate * @return boolean true if within range. */ protected function isValidDate($value) { $minValue = $this->getMinValue(); $maxValue = $this->getMaxValue(); $valid = true; $dateFormat = $this->getDateFormat(); if ($dateFormat !== '') { $formatter = new TSimpleDateFormatter($dateFormat); $value = $formatter->parse($value); if ($minValue !== '') { $valid = $valid && $this->isGreaterThan($value, $formatter->parse($minValue)); } if ($maxValue !== '') { $valid = $valid && $this->isLessThan($value, $formatter->parse($maxValue)); } return $valid; } else { $value = strtotime($value); if ($minValue !== '') { $valid = $valid && $this->isGreaterThan($value, strtotime($minValue)); } if ($maxValue !== '') { $valid = $valid && $this->isLessThan($value, strtotime($maxValue)); } return $valid; } }