Create a date node with node_save().

Last modified: August 7, 2008 - 23:53

The value you save in the node should be the same as the type of date field you're using, either an ISO date or a unix timestamp (or in Drupal 6 it could be a datetime field). The value stored in the database should be the UTC value for the date. So your code will need to take your input values and create a UTC date value in the right format before you create your node. Then do something like:

<?php
$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 you just created. If you have multiple values, repeat the 0 array as many times as needed, incrementing the index each time.

If you want to add date values to an existing node, do something similar:

<?php
$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);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.