You are here

function date_make_date in Date 5.2

Same name and namespace in other branches
  1. 5 date.inc \date_make_date()
  2. 6.2 date_api.module \date_make_date()
  3. 6 date_api.module \date_make_date()

Convert a date of any type or an array of date parts into a valid date object.

Parameters

$date: A date in any format or the string 'now'. @param $timezone Optional, the name of the timezone this date is in, defaults to the user timezone, if set, otherwise the site timezone. Accepts either a timezone name or a timezone object as input. @param $type The type of date provided, could be DATE_ARRAY, DATE_UNIX, DATE_DATETIME, DATE_ISO, or DATE_OBJECT. @param $granularity The granularity of the date value provided. Set this for partial dates so they pass validation and don't get reset to 'now'.

29 calls to date_make_date()
date_combo_validate in date/date_elements.inc
Validate and update a combo element. Don't try this if there were errors before reaching this point.
date_copy_convert_events in date_copy/date_copy.module
date_data_integrity in date/date.install
Progressive update of date information, integrity checking of all date values.
date_days in ./date_api.module
An array of days.
date_days_in_month in ./date_api.module
Identify the number of days in a month for a date.

... See full list

File

./date_api.module, line 636
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_make_date($date, $timezone = NULL, $type = DATE_DATETIME, $granularity = array(
  'year',
  'month',
  'day',
  'hour',
  'minute',
)) {

  // No value or one that can't be used.
  if (empty($date) || is_array($date)) {
    return NULL;
  }

  // Make sure some value is set for the date and timezone even if the
  // site timezone is not yet set up to avoid fatal installation
  // errors.
  if (empty($timezone) || !date_timezone_is_valid($timezone)) {
    $timezone = date_default_timezone_name();
  }

  // Special handling for partial dates that don't need precision.
  $max_granularity = array_pop($granularity);
  if (in_array($max_granularity, array(
    'year',
    'month',
  )) || $type == DATE_ISO || $type == DATE_ARRAY) {
    if ($type == DATE_UNIX) {
      $date = date_convert($date, $type, DATE_DATETIME);
    }
    $date = date_fuzzy_datetime($date);
    $type = DATE_DATETIME;
  }
  if (!date_is_valid($date, $type, $granularity)) {
    $date = 'now';
  }
  if (!empty($timezone) && !empty($date)) {
    if ($date == 'now') {
      return date_create('now', timezone_open($timezone));
    }
    elseif ($datetime = date_convert($date, $type, DATE_DATETIME, $timezone)) {
      return date_create($datetime, timezone_open($timezone));
    }
  }
  return NULL;
}