function DateTimePlus::__construct

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

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

Parameters

string $time: (optional) A date/time string. Defaults to 'now'.

mixed $timezone: (optional) \DateTimeZone object, time zone string or NULL. NULL uses the default system time zone. Defaults to NULL. Note that the $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00). @see http://php.net/manual/datetime.construct.php

array $settings: (optional) Keyed array of settings. Defaults to empty array.

  • langcode: (optional) String two letter language code used to control the result of the format(). Defaults to NULL.
  • 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 core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Constructs a date object.
1 method overrides DateTimePlus::__construct()
DrupalDateTime::__construct in core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Constructs a date object.

File

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

Class

DateTimePlus
Wraps DateTime().

Namespace

Drupal\Component\Datetime

Code

public function __construct($time = 'now', $timezone = NULL, $settings = []) {
    // Unpack settings.
    $this->langcode = !empty($settings['langcode']) ? $settings['langcode'] : NULL;
    // Massage the input values as necessary.
    $prepared_time = $this->prepareTime($time);
    $prepared_timezone = $this->prepareTimezone($timezone);
    try {
        $this->errors = [];
        if (!empty($prepared_time)) {
            $test = date_parse($prepared_time);
            if (!empty($test['errors'])) {
                $this->errors = $test['errors'];
            }
        }
        if (empty($this->errors)) {
            $this->dateTimeObject = new \DateTime($prepared_time, $prepared_timezone);
        }
    } catch (\Exception $e) {
        $this->errors[] = $e->getMessage();
    }
    // Clean up the error messages.
    $this->checkErrors();
}

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