You are here

public function DateObject::toISO in Date 7

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

Create an ISO date from an array of values.

1 call to DateObject::toISO()
DateObject::__construct in date_api/date_api.module
Overridden constructor.

File

date_api/date_api.module, line 502
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Class

DateObject
Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.

Code

public function toISO($arr, $full = FALSE) {

  // Add empty values to avoid errors
  $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;
}