function DateTimePlus::format

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Datetime/DateTimePlus.php \Drupal\Component\Datetime\DateTimePlus::format()
  2. 10 core/lib/Drupal/Component/Datetime/DateTimePlus.php \Drupal\Component\Datetime\DateTimePlus::format()
  3. 11.x core/lib/Drupal/Component/Datetime/DateTimePlus.php \Drupal\Component\Datetime\DateTimePlus::format()

Formats the date for display.

Parameters

string $format: Format accepted by date().

array $settings:

  • timezone: (optional) String timezone name. Defaults to the timezone of the date object.

Return value

string|null The formatted value of the date or NULL if there were construction errors.

2 calls to DateTimePlus::format()
DateTimePlus::render in core/lib/Drupal/Component/Datetime/DateTimePlus.php
Renders the timezone name.
DrupalDateTime::format in core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Overrides format().
1 method overrides DateTimePlus::format()
DrupalDateTime::format in core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Overrides format().

File

core/lib/Drupal/Component/Datetime/DateTimePlus.php, line 687

Class

DateTimePlus
Wraps DateTime().

Namespace

Drupal\Component\Datetime

Code

public function format($format, $settings = []) {
    // If there were construction errors, we can't format the date.
    if ($this->hasErrors()) {
        return;
    }
    // Format the date and catch errors.
    try {
        // Clone the date/time object so we can change the time zone without
        // disturbing the value stored in the object.
        $dateTimeObject = clone $this->dateTimeObject;
        if (isset($settings['timezone'])) {
            $dateTimeObject->setTimezone(new \DateTimeZone($settings['timezone']));
        }
        $value = $dateTimeObject->format($format);
    } catch (\Exception $e) {
        $this->errors[] = $e->getMessage();
    }
    return $value;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.