function date_make_date in Date 6.2
Same name and namespace in other branches
- 5.2 date_api.module \date_make_date()
- 5 date.inc \date_make_date()
- 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'.
33 calls to date_make_date()
- DateAPITestCase::testDateAPI in tests/
date_api.test - date_api_filter_handler::date_filter in includes/
date_api_filter_handler.inc - 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_days in ./
date_api.module - An array of days.
- date_formatter_process in date/
date.module - Helper function for creating formatted date arrays from a formatter.
File
- ./
date_api.module, line 845 - 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',
)) {
// 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 a unix timestamp of '0', since it will fail 'empty' tests below.
if ($date === 0 && $type == DATE_UNIX) {
$date = date_convert($date, $type, DATE_DATETIME, $timezone);
$type = DATE_DATETIME;
}
// No value or one with unexpected array keys.
if (empty($date) || is_array($date) && array_diff($granularity, array_keys($date))) {
return NULL;
}
// Special handling for partial dates that don't need precision.
$granularity_sorted = date_granularity_sorted($granularity);
$max_granularity = end($granularity_sorted);
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;
}