function DateTimePlus::checkErrors

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

Examines getLastErrors() to see what errors to report.

Two kinds of errors are important: anything that DateTime considers an error, and also a warning that the date was invalid. PHP creates a valid date from invalid data with only a warning, 2011-02-30 becomes 2011-03-03, for instance, but we don't want that.

See also

http://php.net/manual/time.getlasterrors.php

1 call to DateTimePlus::checkErrors()
DateTimePlus::__construct in core/lib/Drupal/Component/Datetime/DateTimePlus.php
Constructs a date object set to a requested date and timezone.

File

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

Class

DateTimePlus
Wraps DateTime().

Namespace

Drupal\Component\Datetime

Code

public function checkErrors() {
    $errors = \DateTime::getLastErrors();
    if (!empty($errors['errors'])) {
        $this->errors = array_merge($this->errors, $errors['errors']);
    }
    // Most warnings are messages that the date could not be parsed
    // which causes it to be altered. For validation purposes, a warning
    // as bad as an error, because it means the constructed date does
    // not match the input value.
    if (!empty($errors['warnings'])) {
        $this->errors[] = 'The date is invalid.';
    }
    $this->errors = array_values(array_unique($this->errors));
}

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