You are here

public function DateObject::toISO in Date 7.2

Same name and namespace in other branches
  1. 7.3 date_api/date_api.module \DateObject::toISO()
  2. 7 date_api/date_api.module \DateObject::toISO()

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.

1 call to DateObject::toISO()
DateObject::__construct in date_api/date_api.module
Constructs a date object.

File

date_api/date_api.module, line 834
This module will make the date API available to other modules.

Class

DateObject
Extend PHP DateTime class.

Code

public function toISO(array $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;
}