You are here

public static function DateTimePlus::arrayToISO in Service Container 7

Same name and namespace in other branches
  1. 7.2 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 lib/Drupal/Component/Datetime/DateTimePlus.php
Creates a date object from an array of date parts.

File

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

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;
}