class DrupalDateTime

Extends DateTimePlus().

This class extends the basic component and adds in Drupal-specific handling, like translation of the format() method.

Hierarchy

  • class \Drupal\Component\Datetime\DateTimePlus extends \Drupal\Component\Datetime\DateTime

Expanded class hierarchy of DrupalDateTime

See also

Drupal/Component/Datetime/DateTimePlus.php

25 files declare their use of DrupalDateTime
CommentFormController.php in drupal/core/modules/comment/lib/Drupal/comment/CommentFormController.php
Definition of Drupal\comment\CommentFormController.
CommentPreviewTest.php in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentPreviewTest.php
Definition of Drupal\comment\Tests\CommentPreviewTest.
common.inc in drupal/core/includes/common.inc
Common functions that many Drupal modules will need to reference.
CustomBlockFormController.php in drupal/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
Contains \Drupal\custom_block\CustomBlockFormController.
Date.php in drupal/core/lib/Drupal/Core/Datetime/Date.php
Contains \Drupal\Component\Datetime\Date.

... See full list

1 string reference to 'DrupalDateTime'
DrupalDateTimeTest::getInfo in drupal/core/modules/system/lib/Drupal/system/Tests/Datetime/DrupalDateTimeTest.php
Test information.

File

drupal/core/lib/Drupal/Core/Datetime/DrupalDateTime.php, line 20
Definition of Drupal\Core\Datetime\DrupalDateTime.

Namespace

Drupal\Core\Datetime
View source
class DrupalDateTime extends DateTimePlus {

  /**
   * Constructs a date object.
   *
   * @param mixed $time
   *   A DateTime object, a date/input_time_adjusted string, a unix timestamp,
   *   or an array of date parts, like ('year' => 2014, 'month => 4).
   *   Defaults to 'now'.
   * @param mixed $timezone
   *   PHP DateTimeZone object, string or NULL allowed.
   *   Defaults to NULL.
   * @param string $format
   *   PHP date() type format for parsing the input. This is recommended
   *   to use things like negative years, which php's parser fails on, or
   *   any other specialized input with a known format. If provided the
   *   date will be created using the createFromFormat() method.
   *   Defaults to NULL.
   *   @see http://us3.php.net/manual/en/datetime.createfromformat.php
   * @param array $settings
   *   - validate_format: (optional) Boolean choice to validate the
   *     created date using the input format. The format used in
   *     createFromFormat() allows slightly different values than format().
   *     Using an input format that works in both functions makes it
   *     possible to a validation step to confirm that the date created
   *     from a format string exactly matches the input. This option
   *     indicates the format can be used for validation. Defaults to TRUE.
   *   - 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.
   *   - debug: (optional) Boolean choice to leave debug values in the
   *     date object for debugging purposes. Defaults to FALSE.
   */
  public function __construct($time = 'now', $timezone = NULL, $format = NULL, $settings = array()) {

    // We can set the langcode and country using Drupal values.
    $settings['langcode'] = !empty($settings['langcode']) ? $settings['langcode'] : language(Language::TYPE_INTERFACE)->langcode;
    $settings['country'] = !empty($settings['country']) ? $settings['country'] : config('system.date')
      ->get('country.default');

    // Instantiate the parent class.
    parent::__construct($time, $timezone, $format, $settings);
  }

  /**
   * Overrides prepareTimezone().
   *
   * Override basic component timezone handling to use Drupal's
   * knowledge of the preferred user timezone.
   */
  protected function prepareTimezone($timezone) {
    $user_timezone = drupal_get_user_timezone();
    if (empty($timezone) && !empty($user_timezone)) {
      $timezone = $user_timezone;
    }
    parent::prepareTimezone($timezone);
  }

  /**
   * Overrides format().
   *
   * Uses the IntlDateFormatter to display the format, if possible.
   * Adds an optional array of settings that provides the information
   * the IntlDateFormatter will need.
   *
   * @param string $format
   *   A format string using either PHP's date() or the
   *   IntlDateFormatter() format.
   * @param 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 string
   *   The formatted value of the date.
   */
  public function format($format, $settings = array()) {
    $format_string_type = isset($settings['format_string_type']) ? $settings['format_string_type'] : static::PHP;
    $settings['calendar'] = !empty($settings['calendar']) ? $settings['calendar'] : $this->calendar;
    $settings['langcode'] = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode;
    $settings['country'] = !empty($settings['country']) ? $settings['country'] : $this->country;

    // Format the date and catch errors.
    try {

      // If we have what we need to use the IntlDateFormatter, do so.
      if ($this
        ->canUseIntl($settings['calendar'], $settings['langcode'], $settings['country']) && $format_string_type == parent::INTL) {
        $value = parent::format($format, $settings);
      }
      else {

        // Encode markers that should be translated. 'A' becomes
        // '\xEF\AA\xFF'. xEF and xFF are invalid UTF-8 sequences,
        // and we assume they are not in the input string.
        // Paired backslashes are isolated to prevent errors in
        // read-ahead evaluation. The read-ahead expression ensures that
        // A matches, but not \A.
        $format = preg_replace(array(
          '/\\\\\\\\/',
          '/(?<!\\\\)([AaeDlMTF])/',
        ), array(
          "",
          "",
        ), $format);

        // Call date_format().
        $format = parent::format($format);

        // Pass the langcode to _format_date_callback().
        _format_date_callback(NULL, $settings['langcode']);

        // Translate the marked sequences.
        $value = preg_replace_callback('/\\xEF([AaeDlMTF]?)(.*?)\\xFF/', '_format_date_callback', $format);
      }
    } catch (\Exception $e) {
      $this->errors[] = $e
        ->getMessage();
    }
    return $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateTimePlus::$calendar protected property The value of the calendar setting passed to the constructor.
DateTimePlus::$country protected property The value of the country code passed to the constructor.
DateTimePlus::$dateParts protected static property An array of possible date parts.
DateTimePlus::$errors protected property An array of errors encountered when creating this date.
DateTimePlus::$inputFormatAdjusted protected property The prepared format, if provided.
DateTimePlus::$inputFormatRaw protected property The value of the format passed to the constructor.
DateTimePlus::$inputTimeAdjusted protected property The prepared time, without timezone, for this date.
DateTimePlus::$inputTimeRaw protected property The value of the time value passed to the constructor.
DateTimePlus::$inputTimeZoneAdjusted protected property The prepared timezone object used to construct this date.
DateTimePlus::$inputTimeZoneRaw protected property The value of the timezone passed to the constructor.
DateTimePlus::$langcode protected property The value of the language code passed to the constructor.
DateTimePlus::arrayToISO public static function Creates an ISO date from an array of values.
DateTimePlus::CALENDAR constant
DateTimePlus::canUseIntl public function Tests whether the IntlDateFormatter can be used.
DateTimePlus::checkArray public static function Checks that arrays of date parts will create a valid date.
DateTimePlus::checkErrors public function Examines getLastErrors() to see what errors to report.
DateTimePlus::constructFallback protected function Creates a date when none of the other methods are appropriate.
DateTimePlus::constructFromArray protected function Creates a date object from an array of date parts.
DateTimePlus::constructFromFormat protected function Creates a date object from an input format.
DateTimePlus::constructFromObject protected function Creates a date object from an input date object.
DateTimePlus::constructFromTimestamp protected function Creates a date object from timestamp input.
DateTimePlus::datePad public static function Pads date parts with zeros.
DateTimePlus::FORMAT constant
DateTimePlus::getErrors public function Retrieves error messages.
DateTimePlus::hasErrors public function Detects if there were errors in the processing of this date.
DateTimePlus::inputIsArray public function Checks if input is an array of date parts.
DateTimePlus::inputIsFormat public function Checks if input is a string with an expected format.
DateTimePlus::inputIsObject public function Checks whether input is a DateTime object.
DateTimePlus::inputIsTimestamp public function Checks whether input time seems to be a timestamp.
DateTimePlus::INTL constant
DateTimePlus::PHP constant
DateTimePlus::prepareArray public static function Creates a complete array from a possibly incomplete array of date parts.
DateTimePlus::prepareFormat protected function Prepares the input format value.
DateTimePlus::prepareTime protected function Prepares the input time value.
DateTimePlus::__toString public function Implements __toString() for dates.
DrupalDateTime::format public function Overrides format(). Overrides DateTimePlus::format
DrupalDateTime::prepareTimezone protected function Overrides prepareTimezone(). Overrides DateTimePlus::prepareTimezone
DrupalDateTime::__construct public function Constructs a date object. Overrides DateTimePlus::__construct