You are here

public function DateTimePlus::__construct in Service Container 7.2

Same name and namespace in other branches
  1. 7 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.

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.

File

lib/Drupal/Component/Datetime/DateTimePlus.php, line 246
Contains \Drupal\Component\Datetime\DateTimePlus.

Class

DateTimePlus
Wraps DateTime().

Namespace

Drupal\Component\Datetime

Code

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

  // 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 {
    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();
  $this->errors = array_unique($this->errors);
}