On this page
Create a date node with node_save().
To save a node using node_save() that has a date field the field needs to be formatted in the same way as the content type, either ISO date, UNIX timestamp or in Drupal 6 a datetime field.
The value stored in the database should be the UTC value for the date. PHP code will need to take the input values and create a UTC date value in the right format before creating the node. Then do something like:
$node = new StdClass();
$node->type = 'story'; // The content type this field is used in
$node->field_date = array( // The name of the field
0 => array(
'value' => $utc_value,
'value2' => $utc_value2, // If you are using an optional or required 'to date', if you have no 'to date' leave this out
'timezone' => 'America/Chicago', // Only if you're using a 'date' timezone, otherwise leave this out
'offset' => -18000, // Only if you're using a 'date' timezone, otherwise leave this out
'offset2' => -18000, // Only if you're using a 'date' timezone and have a 'to date', otherwise leave this out
),
);
node_submit($node); // This step is needed only in Drupal 5, not in Drupal 6.
node_save($node);
After saving the node, $node->nid will contain the id of the node created. If there are multiple values, repeat the 0 array as many times as needed, incrementing the index each time.
To add date values to an existing node, do something similar:
$node = node_load(73);
$node->field_date = array( // The name of the field
0 => array(
'value' => $utc_value,
'value2' => $utc_value2, // If you are using an optional or required 'to date', if you have no 'to date' leave this out
'timezone' => 'America/Chicago', // Only if you're using a 'date' timezone, otherwise leave this out
'offset' => -18000, // Only if you're using a 'date' timezone, otherwise leave this out
'offset2' => -18000, // Only if you're using a 'date' timezone and have a 'to date', otherwise leave this out
),
);
node_submit($node); // This step is needed only in Drupal 5, not in Drupal 6.
node_save($node);
Converting to UTC Format
Just a bit of clarification on the format of the UTC value specified in the above code.
The variable $utc_value should be a string using the format 'Y-m-d\TH:i:s'
This function converts user entered date/time values in the format mm/dd/yyyy hh:mm:ss AM to the UTC value required by the DateTime module.
function _mymodule_convert_text_date_to_utc_string($text_date) {
// First get the user's (or system default) timezone info
global $user;
$user_timezone_name = date_default_timezone_name(TRUE);
$user_timezone = date_default_timezone(TRUE);
// Convert the user entered date into a PHP 5 DateTime object
$local_date = new DateTime($text_date);
// Reformat the user entered date into an ISO date that date_make_date() will accept
$iso_date_string = date_format_date($local_date, 'custom', 'Y-m-d\TH:i:s');
// Create a DateTime object with the user's timezone info
$utc_date = date_make_date($iso_date_string, $user_timezone_name, DATE_DATETIME);
// Change the timezone to UTC
date_timezone_set($utc_date, timezone_open('UTC'));
// Format the UTC version of the DateTime for use in node->save()
$utc_date_string = date_format_date($utc_date, 'custom', 'Y-m-d\TH:i:s');
return $utc_date_string;
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion