function DateTimePlus::arrayToISO

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

Creates an ISO date from an array of values.

Parameters

array $array: An array of date values keyed by date part.

bool $force_valid_date: (optional) Whether to force a full date by filling in missing values. Defaults to FALSE.

Return value

string The date as an ISO string.

1 call to DateTimePlus::arrayToISO()
DateTimePlus::createFromArray in core/lib/Drupal/Component/Datetime/DateTimePlus.php
Creates a date object from an array of date parts.

File

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

Class

DateTimePlus
Wraps DateTime().

Namespace

Drupal\Component\Datetime

Code

public static function arrayToISO($array, $force_valid_date = FALSE) {
    $array = static::prepareArray($array, $force_valid_date);
    $input_time = '';
    if ($array['year'] !== '') {
        $input_time = static::datePad(intval($array['year']), 4);
        if ($force_valid_date || $array['month'] !== '') {
            $input_time .= '-' . static::datePad(intval($array['month']));
            if ($force_valid_date || $array['day'] !== '') {
                $input_time .= '-' . static::datePad(intval($array['day']));
            }
        }
    }
    if ($array['hour'] !== '') {
        $input_time .= $input_time ? 'T' : '';
        $input_time .= static::datePad(intval($array['hour']));
        if ($force_valid_date || $array['minute'] !== '') {
            $input_time .= ':' . static::datePad(intval($array['minute']));
            if ($force_valid_date || $array['second'] !== '') {
                $input_time .= ':' . static::datePad(intval($array['second']));
            }
        }
    }
    return $input_time;
}

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