You are here

function date_fuzzy_datetime in Date 6.2

Same name and namespace in other branches
  1. 5.2 date_api.module \date_fuzzy_datetime()
  2. 6 date_api.module \date_fuzzy_datetime()

Create valid datetime value from incomplete ISO dates or arrays.

7 calls to date_fuzzy_datetime()
date_api_filter_handler::date_filter in includes/date_api_filter_handler.inc
date_convert in ./date_api.module
Date conversion helper function.
date_formatter_process in date/date.module
Helper function for creating formatted date arrays from a formatter.
date_local_date in date/date_elements.inc
Create local date object.
date_make_date in ./date_api.module
Convert a date of any type or an array of date parts into a valid date object.

... See full list

File

./date_api.module, line 1335
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.

Code

function date_fuzzy_datetime($date) {

  // A text ISO date, like MMMM-YY-DD HH:MM:SS
  if (!is_array($date)) {
    $date = date_iso_array($date);
  }
  elseif (array_key_exists('date', $date) || array_key_exists('time', $date)) {
    $date_part = array_key_exists('date', $date) ? $date['date'] : '';
    $time_part = array_key_exists('time', $date) ? $date['time'] : '';
    $date = date_iso_array(trim($date_part . ' ' . $time_part));
  }

  // Otherwise date must in in format:
  //  array('year' => YYYY, 'month' => MM, 'day' => DD).
  if (empty($date['year'])) {
    $date['year'] = date('Y');
  }
  if (empty($date['month'])) {
    $date['month'] = 1;
  }
  if (empty($date['day'])) {
    $date['day'] = 1;
  }
  foreach (array(
    'hour',
    'minute',
    'second',
  ) as $part) {
    if (empty($date[$part])) {
      $date[$part] = 0;
    }
  }
  $value = date_pad($date['year'], 4) . '-' . date_pad($date['month']) . '-' . date_pad($date['day']) . ' ' . date_pad($date['hour']) . ':' . date_pad($date['minute']) . ':' . date_pad($date['second']);
  return $value;
}