I have tried using both date and datestamp. The widget is selector. The timezone is site's timezone. "America/Los Angeles". Not using the required or multiple options.

I'm wish to use node_save to create my node and include date using date cck. Don't know how to make it work to save the correct date. Right now, everything I'm trying does not result in the correct date being displayed. Either nothing is displayed or the date is 1969 Dec 31 - 4:00pm


  $title = 'Test Title';
  $body = 'Test Body';

  $created = 1206936000; // datestamp
  $created2 = '2007-08-01 01:00'; // date

  $node = new stdClass();
  $node = array();
  $node['type'] = 'brix';
  $node['uid'] = 1;
  $node['title'] = $title;
  $node['body'] = $body;
  $node['field_created'] = array(0 => array('value' => $created));
  $node['field_created2'] = array(0 => array('value' => $created2));

  if ($node = node_submit($node)) {
    node_save($node);
  }

How do I make this work? Thanks!

Comments

somebodysysop’s picture

Also tried this to no effect:

  $created = date_make_date('2008-03-23 21:29:09.0', 'none', 'db', DATE_UNIX);
  $created2 = date_make_date('2008-03-23 21:29:09.0', 'none', 'db', DATE_ISO);
jmspldnl’s picture

I was having a similar problem. I'm not real sure what you are trying to do here, but I got the correct date to display by using the date function included in php. Looked like this in my code.

print date('m-d-y', $node->field_date[0]['value']);

Not sure if this helps with your problem, but it did help me get the date to display correctly.

somebodysysop’s picture

Thanks for the response. The first one in over a week. What I am trying to do is insert a date value into $node->field_date[0]['value'] as opposed to printing out an existing value. I'm can't seem to figure out how to do this.

mradcliffe’s picture

Version: 5.x-1.8 » 6.x-2.0-beta3

I am having a bit of trouble with this as well. There doesn't seem to be any documentation on how exactly to set $node->field_MYDATEFIELD[0]['value']. It seems like this could either be a unix timestamp or iso date string per this snippet from date/date_content_generate.inc:

50 $node_field[$i]['value'] = date_format($start, $format);

However doing so always gives me bogus dates. What is the correct format? Essentially I'm trying to do

$node->field_release[0]['value'] = strtotime('Jul 10, 2008'); //this has a correct unix timestamp per debug, but does not insert correctly by date module

or

$mydate = date_create(strtotime('Jul 10, 2008'));  // this returns the correct unix timestamp for that date, correct data.
$node->field_release[0]['value'] = date_format($date,DATE_FORMAT_UNIX); // this is bogus?

These do not work. What is the correct API usage for saving nodes?

Any help would be appreciated before I stop using date and make my own custom input filter for a regular CCK integer field

karens’s picture

Version: 6.x-2.0-beta3 » 5.x-1.8
Status: Active » Fixed

Don't switch an issue from Date 5.1 to 6.2 - the 5.1 code is totally and completely different from the 5.2 code and the 6.2 code is totally and completely different from either 5.1 or 5.2. And CCK works differently in 6.2 than it works in Drupal 5.

The key to getting node_save working is to look at the way it is done in the Date Copy module for each version, since that's exactly what Date Copy does. In the Drupal 5 version you should be able to create a node array as noted in the original issue, but the format of the submitted value should be exactly the right one for the type of field, either a unix timestamp or an iso date (which must include hours, minutes, and seconds, along with the 'T', as in YYYY-MM-DDTHH:MM:SS). I can't tell what type of date you're importing into so I don't know which should be producing the right results.

The example in #1 won't work because the date values submitted do not match the type of date you are telling date_make_date() you are creating.

It's also important to use node_submit($node) before node_save($node), which was done in some of the examples, but possibly not with the right date values.

The D6 issue takes a different solution and I'm still working on the D6 code, so I wouldn't be surprised if it isn't working right yet. Plus CCK for D6 is totally different so there are other changes that may be needed to adapt to the differences between CCK for D6 and CCK for D5.

I haven't been answering 5.1 issues because I'm spending nearly all day every day working on 5.2 and 6.2 and there aren't enough hours in the day to do more than that.

If you have problems in 5.1 I recommend moving from 5.1 to 5.2 -- it's getting very close to becoming the recommended version and there are lots of problems in 5.1 that just can't be easily solved, which is the reason for creating 5.2.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

somebodysysop’s picture

Status: Closed (fixed) » Active

I looked at date_copy.module but just can't figure it out. Can someone please just give me an example:

I've got a date in a string that looks like this:

$date = "2008-07-31";
$time = "8:30pm";

How do I create an event start date:

$node->event_start = ?
somebodysysop’s picture

Status: Active » Fixed

This is what I came up with:

    $datestring = '2008-10-11';
    $datestring = $datestring . ' 21:30:00';
    $unixdate = strtotime($datestring);

    $node['event_start'] = $unixdate;
    $node['timezone'] = variable_get('date_default_timezone', 0);

Don't know if this is the correct way to do it, but it works.

karens’s picture

This is a closed issue. It's just an accident that I found your question.

You're looking at an example of how to create an Event module date. That won't work.

I'm assuming you are using a datestamp field since you're converting your value to a timestamp, and that you have a field called field_date. You write whatever code you need to convert your starting values into a timestamp in UTC, then:

$node = new StdClass();
$node->type = 'story';
$node->field_date = array(
  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);
node_save($node);

After saving it, $node->nid will have the id of the new node you created. If you are using multiple values, you can duplicate the arrays for index 1, 2, etc.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.