Formats the date for display.
Uses the IntlDateFormatter to display the format, if possible. Adds an optional array of settings that provides the information the IntlDateFormatter will need.
string $format: A format string using either PHP's date() or the IntlDateFormatter() format.
array $settings:
string The formatted value of the date.
public function format($format, $settings = array()) {
// If there were construction errors, we can't format the date.
if ($this
->hasErrors()) {
return;
}
$format_string_type = isset($settings['format_string_type']) ? $settings['format_string_type'] : static::PHP;
$langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode;
$country = !empty($settings['country']) ? $settings['country'] : $this->country;
$calendar = !empty($settings['calendar']) ? $settings['calendar'] : $this->calendar;
// Format the date and catch errors.
try {
// If we have what we need to use the IntlDateFormatter, do so.
if ($this
->canUseIntl($calendar, $langcode, $country) && $format_string_type == static::INTL) {
// Construct the $locale variable needed by the IntlDateFormatter.
$locale = $langcode . '_' . $country;
// If we have information about a calendar, add it.
if (!empty($calendar) && $calendar != static::CALENDAR) {
$locale .= '@calendar=' . $calendar;
}
// If we're working with a non-gregorian calendar, indicate that.
$calendar_type = \IntlDateFormatter::GREGORIAN;
if ($calendar != self::CALENDAR) {
$calendar_type = \IntlDateFormatter::TRADITIONAL;
}
$date_type = !empty($settings['date_type']) ? $settings['date_type'] : \IntlDateFormatter::FULL;
$time_type = !empty($settings['time_type']) ? $settings['time_type'] : \IntlDateFormatter::FULL;
$timezone = !empty($settings['timezone']) ? $settings['timezone'] : $this
->getTimezone()
->getName();
$formatter = new \IntlDateFormatter($locale, $date_type, $time_type, $timezone, $calendar_type, $format);
$lenient = !empty($settings['lenient']) ? $settings['lenient'] : FALSE;
$formatter
->setLenient($lenient);
$value = $formatter
->format($this);
}
else {
$value = parent::format($format);
}
} catch (\Exception $e) {
$this->errors[] = $e
->getMessage();
}
return $value;
}