1.20.x date.class.inc public BackdropDateTime::toISO($arr, $full = FALSE)

Creates an ISO date from an array of values.

Parameters

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

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

Return value

string: The date formatted as an ISO-8601 string.

File

includes/date.class.inc, line 636

Class

BackdropDateTime
Extends PHP DateTime class for use with Backdrop.

Code

public function toISO($arr, $full = FALSE) {
  // Add empty values to avoid errors. The empty values must create a valid
  // date or we will get date slippage, i.e. a value of 2011-00-00 will get
  // interpreted as November of 2010 by PHP.
  if ($full) {
    $arr += array(
      'year' => 0,
      'month' => 1,
      'day' => 1,
      'hour' => 0,
      'minute' => 0,
      'second' => 0,
    );
  }
  else {
    $arr += array(
      'year' => '',
      'month' => '',
      'day' => '',
      'hour' => '',
      'minute' => '',
      'second' => '',
    );
  }
  $datetime = '';
  if ($arr['year'] !== '') {
    $datetime = date_pad(intval($arr['year']), 4);
    if ($full || $arr['month'] !== '') {
      $datetime .= '-' . date_pad(intval($arr['month']));
      if ($full || $arr['day'] !== '') {
        $datetime .= '-' . date_pad(intval($arr['day']));
      }
    }
  }
  if ($arr['hour'] !== '') {
    $datetime .= $datetime ? 'T' : '';
    $datetime .= date_pad(intval($arr['hour']));
    if ($full || $arr['minute'] !== '') {
      $datetime .= ':' . date_pad(intval($arr['minute']));
      if ($full || $arr['second'] !== '') {
        $datetime .= ':' . date_pad(intval($arr['second']));
      }
    }
  }
  return $datetime;
}