public function DateTimePlus::__construct

Constructs a date object set to a requested date and timezone.

Parameters

mixed $time: A DateTime object, a date/time string, a unix timestamp, or an array of date parts, like ('year' => 2014, 'month => 4). Defaults to 'now'.

mixed $timezone: PHP DateTimeZone object, string or NULL allowed. Defaults to NULL.

string $format: PHP date() type format for parsing the input. This is recommended for 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

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.
1 call to DateTimePlus::__construct()
DrupalDateTime::__construct in drupal/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Constructs a date object.
1 method overrides DateTimePlus::__construct()
DrupalDateTime::__construct in drupal/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Constructs a date object.

File

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

Class

DateTimePlus
Extends DateTime().

Namespace

Drupal\Component\Datetime

Code

public function __construct($time = 'now', $timezone = NULL, $format = NULL, $settings = array()) {

  // Unpack settings.
  $this->validateFormat = !empty($settings['validate_format']) ? $settings['validate_format'] : TRUE;
  $this->langcode = !empty($settings['langcode']) ? $settings['langcode'] : NULL;
  $this->country = !empty($settings['country']) ? $settings['country'] : NULL;
  $this->calendar = !empty($settings['calendar']) ? $settings['calendar'] : static::CALENDAR;

  // Store the original input so it is available for validation.
  $this->inputTimeRaw = $time;
  $this->inputTimeZoneRaw = $timezone;
  $this->inputFormatRaw = $format;

  // Massage the input values as necessary.
  $this
    ->prepareTime($time);
  $this
    ->prepareTimezone($timezone);
  $this
    ->prepareFormat($format);

  // Create a date as a clone of an input DateTime object.
  if ($this
    ->inputIsObject()) {
    $this
      ->constructFromObject();
  }
  elseif ($this
    ->inputIsArray()) {
    $this
      ->constructFromArray();
  }
  elseif ($this
    ->inputIsTimestamp()) {
    $this
      ->constructFromTimestamp();
  }
  elseif ($this
    ->inputIsFormat()) {
    $this
      ->constructFromFormat();
  }
  else {
    $this
      ->constructFallback();
  }

  // Clean up the error messages.
  $this
    ->checkErrors();
  $this->errors = array_unique($this->errors);

  // Now that we've validated the input, clean up the extra values.
  if (empty($settings['debug'])) {
    unset($this->inputTimeRaw, $this->inputTimeAdjusted, $this->inputTimeZoneRaw, $this->inputTimeZoneAdjusted, $this->inputFormatRaw, $this->inputFormatAdjusted, $this->validateFormat);
  }
}