You are here

public static function DateTimePlus::createFromArray in Service Container 7

Same name and namespace in other branches
  1. 7.2 lib/Drupal/Component/Datetime/DateTimePlus.php \Drupal\Component\Datetime\DateTimePlus::createFromArray()

Creates a date object from an array of date parts.

Converts the input value into an ISO date, forcing a full ISO date even if some values are missing.

Parameters

array $date_parts: An array of date parts, like ('year' => 2014, 'month => 4).

mixed $timezone: (optional) \DateTimeZone object, time zone string or NULL. NULL uses the default system time zone. Defaults to NULL.

array $settings: (optional) A keyed array for settings, suitable for passing on to __construct().

Return value

static A new \Drupal\Component\DateTimePlus object based on the parameters passed in.

File

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

Class

DateTimePlus
Wraps DateTime().

Namespace

Drupal\Component\Datetime

Code

public static function createFromArray(array $date_parts, $timezone = NULL, $settings = array()) {
  $date_parts = static::prepareArray($date_parts, TRUE);
  if (static::checkArray($date_parts)) {

    // Even with validation, we can end up with a value that the
    // DateTime class won't handle, like a year outside the range
    // of -9999 to 9999, which will pass checkdate() but
    // fail to construct a date object.
    $iso_date = static::arrayToISO($date_parts);
    return new static($iso_date, $timezone, $settings);
  }
  else {
    throw new \Exception('The array contains invalid values.');
  }
}