You are here

function webform_date_array in Webform 6.3

Same name and namespace in other branches
  1. 7.4 webform.module \webform_date_array()
  2. 7.3 webform.module \webform_date_array()

Convert an ISO 8601 date or time into an array.

This converts full format dates or times. Either a date or time may be provided, in which case only those portions will be returned. Dashes and colons must be used, never implied.

Formats: Dates: YYYY-MM-DD Times: HH:MM:SS Datetimes: YYYY-MM-DDTHH:MM:SS

Parameters

$string: An ISO 8601 date, time, or datetime.

$type: If wanting only specific fields back, specify either "date" or "time". Leaving empty will return an array with both date and time keys, even if some are empty. Returns an array with the following keys:

  • year
  • month
  • day
  • hour (in 24hr notation)
  • minute
  • second
9 calls to webform_date_array()
webform_expand_date in components/date.inc
Form API #process function for Webform date fields.
webform_expand_time in components/time.inc
Form API #process function for Webform time fields.
_webform_analysis_date in components/date.inc
Implements _webform_analysis_component().
_webform_analysis_time in components/time.inc
Implements _webform_analysis_component().
_webform_csv_data_time in components/time.inc
Implements _webform_csv_data_component().

... See full list

File

./webform.module, line 3473

Code

function webform_date_array($string, $type = NULL) {
  $pattern = '/((\\d{4}?)-(\\d{2}?)-(\\d{2}?))?(T?(\\d{2}?):(\\d{2}?):(\\d{2}?))?/';
  $matches = array();
  preg_match($pattern, $string, $matches);
  $matches += array_fill(0, 9, '');
  $return = array();

  // Check for a date string.
  if ($type == 'date' || !isset($type)) {
    $return['year'] = $matches[2] !== '' ? (int) $matches[2] : '';
    $return['month'] = $matches[3] !== '' ? (int) $matches[3] : '';
    $return['day'] = $matches[4] !== '' ? (int) $matches[4] : '';
  }

  // Check for a time string.
  if ($type == 'time' || !isset($type)) {
    $return['hour'] = $matches[6] !== '' ? (int) $matches[6] : '';
    $return['minute'] = $matches[7] !== '' ? (int) $matches[7] : '';
    $return['second'] = $matches[8] !== '' ? (int) $matches[8] : '';
  }
  return $return;
}