Drupal 7 node_save设置日期

时间:2012-05-23 14:27:45

标签: drupal-7 nodes

我正在从xml自动创建节点,我需要为它们设置创建日期。我试过

<?php 
   ...
   $node->created = $date //<- unix timestamp, $node is my node object
   $node->changed = $other_date; 
   ...
?>

但没有运气,它设定了当前日期。 有任何想法吗? 感谢

-edit -

$newNode = (object) NULL;
$newNode->type = 'blog';
$newNode->title = $title;
$newNode->uid = 1;
$newNode->status = 1;
$newNode->comment = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;
$newNode->body['und'][0] = array(
                                'value' => $body,
                                'format' => 'full_html');
$newNode->log      = 'Auto Imported Node';
$newNode->language = LANGUAGE_NONE;

// add fields
$newNode->field_description[LANGUAGE_NONE][0]['value'] = $description;
$newNode->field_byline[LANGUAGE_NONE][0]['value'] = $byline;
$newNode->field_small_image[LANGUAGE_NONE][0]['value'] = $smallimg;
$newNode->field_large_image[LANGUAGE_NONE][0]['value'] = $largeimg;


// save node
$newNode->created = $pd; // i've tried with commenting this line out, too
$newNode->date = $pd;
node_save($newNode);

2 个答案:

答案 0 :(得分:2)

它适用于$node->created,但无法使用Drupal API设置$node->changed

如果您使用的node_submit()功能使用strtotime($node->date)来设置$node->created,则需要将$node->date设置为字符串日期。

$node->date = $date_string;

答案 1 :(得分:0)

@Zoltan:谢谢,你是对的!当我使用时:

$node->date = '2005-05-06 08:17:27';
node_save($node);

我在保存后得到:

[created] => current timestamp
[changed] => current timestamp

但是在node_save之前添加行:

$node = node_submit($node);

我在保存后得到:

[created] => 1115360247
[changed] => current timestamp

并在节点页面上显示“已创建”日期