function date_set_date in Date 5
Function to set local and db date parts in the date object
Parameters
$date - the date object: @param $value - the date/time value to set @param $timezone - 'GMT', 'none', or timezone name - the timezone of this value
- the 'none' option will do no timezone conversions, dates will be stored exactly as entered
@param $type - db or local, the part of the date object to set
- if you supply a local value and timezone, the function will create the related db values
@param $format - DATE_UNIX or DATE_ISO, the format of the provided value
- if you supply a unix timestamp, the date object will also create an iso version of the date, and vice versa
@param $reset - force a reset of the provided value even if it already exists
6 calls to date_set_date()
- date_convert_timezone in ./
date.inc - Timezone conversion function
- date_make_date in ./
date.inc - Function to create a date object
- date_plus_period in ./
date_views.inc - Compute min and max dates for a P value
- date_selector_make_dbdate in ./
date.inc - Construct a value to save to the database from the date selector
- date_select_input in ./
date.inc - Flexible Date/Time Drop-Down Selector
File
- ./
date.inc, line 232 - Date/time API functions
Code
function date_set_date(&$date, $value, $timezone = 'GMT', $type = 'db', $format = DATE_ISO, $reset = FALSE, $display_errors = FALSE) {
if (trim($value) == '') {
// make new blank date
$date = date_make_date();
return TRUE;
}
// Turn date-only ISO date into complete ISO date,
// i.e. 2007-10-01 becomes 2007-10-01T00:00:00.
if ($format == DATE_ISO && strlen($value) < 19) {
$value = substr($value . 'T00:00:00', 0, 19);
}
// store the starting value of the date object in case there are errors
$old_date = $date;
$error = array();
if (trim($value) === 'ERROR') {
$error[] = 'value';
}
if (!$error && $format == DATE_UNIX) {
$date->{$type}->timestamp = $value;
$parts = date_unix2array($value);
if ($parts === 'ERROR') {
$error[] = 'unix2array';
}
else {
$date->{$type}->parts = $parts;
}
$iso = date_unix2iso($value);
if ($iso === 'ERROR') {
$error[] = 'unix2iso';
}
else {
$date->{$type}->iso = $iso;
}
}
elseif (!$error && $format == DATE_ISO) {
$date->{$type}->iso = $value;
$parts = date_iso2array($value);
if ($parts === 'ERROR') {
$error[] = 'iso2array';
}
else {
$date->{$type}->parts = $parts;
}
$unix = date_iso2unix($value);
if ($unix === 'ERROR') {
$error[] = 'iso2unix';
}
else {
$date->{$type}->timestamp = $unix;
}
}
else {
$error[] = $format;
}
if (!$error && $type == 'local' && (!$date->db->iso || $reset)) {
if (!date_no_conversion($date) && !empty($timezone)) {
$date->local->timezone = $timezone;
// if the local value was submitted, go ahead and compute the db part of the date
date_convert_timezone($date, $date->local->timezone, 'GMT', 'db');
}
else {
$date->db = $date->local;
}
}
elseif (!$error && $type == 'db' && $date->local->iso && (!$date->local->iso || $reset)) {
if (!date_no_conversion($date) && !empty($timezone)) {
// compute local value if the db value was submitted
// and the local value has not been created and there is a local timezone
date_convert_timezone($date, 'GMT', $date->local->timezone, 'local');
}
else {
$date->local = $date->db;
}
}
if ($error) {
// if unable to set date to this value, revert to previous date settings and show error message
$date = $old_date;
if ($display_errors) {
drupal_set_message(t('Unable to set date. ') . implode(', ', $error));
}
return FALSE;
}
return TRUE;
}