public function DateTimePlus::format

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.

Parameters

string $format: A format string using either PHP's date() or the IntlDateFormatter() format.

array $settings:

  • format_string_type: (optional) DateTimePlus::PHP or DateTimePlus::INTL. Identifies the pattern used by the format string. When using the Intl formatter, the format string must use the Intl pattern, which is different from the pattern used by the DateTime format function. Defaults to DateTimePlus::PHP.
  • timezone: (optional) String timezone name. Defaults to the timezone of the date object.
  • langcode: (optional) String two letter language code to construct the locale string by the intlDateFormatter class. Used to control the result of the format() method if that class is available. Defaults to NULL.
  • country: (optional) String two letter country code to construct the locale string by the intlDateFormatter class. Used to control the result of the format() method if that class is available. Defaults to NULL.
  • calendar: (optional) String calendar name to use for the date, Defaults to DateTimePlus::CALENDAR.
  • date_type: (optional) Integer date type to use in the formatter, defaults to IntlDateFormatter::FULL.
  • time_type: (optional) Integer date type to use in the formatter, defaults to IntlDateFormatter::FULL.
  • lenient: (optional) Boolean choice of whether or not to use lenient processing in the intl formatter. Defaults to FALSE;

Return value

string The formatted value of the date.

2 calls to DateTimePlus::format()
DateTimePlus::__toString in drupal/core/lib/Drupal/Component/Datetime/DateTimePlus.php
Implements __toString() for dates.
DrupalDateTime::format in drupal/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Overrides format().
1 method overrides DateTimePlus::format()
DrupalDateTime::format in drupal/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Overrides format().

File

drupal/core/lib/Drupal/Component/Datetime/DateTimePlus.php, line 683
Definition of Drupal\Component\Datetime\DateTimePlus

Class

DateTimePlus
Extends DateTime().

Namespace

Drupal\Component\Datetime

Code

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;
  $timezone = !empty($settings['timezone']) ? $settings['timezone'] : $this
    ->getTimezone()
    ->getName();
  $lenient = !empty($settings['lenient']) ? $settings['lenient'] : FALSE;

  // Format the date and catch errors.
  try {

    // If we have what we need to use the IntlDateFormatter, do so.
    if ($this
      ->canUseIntl() && $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;
      $formatter = new \IntlDateFormatter($locale, $date_type, $time_type, $timezone, $calendar_type);
      $formatter
        ->setLenient($lenient);
      $value = $formatter
        ->format($format);
    }
    else {
      $value = parent::format($format);
    }
  } catch (\Exception $e) {
    $this->errors[] = $e
      ->getMessage();
  }
  return $value;
}