public function DateTimePlus::format

public DateTimePlus::format($format, $settings = array())

Formats the date for display.

Parameters

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

array $settings:

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

Return value

string The formatted value of the date.

File

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

Class

DateTimePlus
Wraps 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 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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Datetime!DateTimePlus.php/function/DateTimePlus::format/8.1.x