0

I'm trying go buid a node who is updated every day updating a field 'field_data' to the current date.

This is my initial code:

function actomodule_cron(){ 
    $nid = 58;
    $node = node_load($nid);
    $simpleDate = time();

    $node->field_data['und'][0]['value'] =  $simpleDate;
    node_save($node);
}

but it didn't work so I add a watchdod print

watchdog('node', print_r($node->field_data['und'][0], true));

who give me this

Array ( 
[value] => 1402408294 
[timezone] => Europe/Paris 
[timezone_db] => Europe/Paris 
[date_type] => datestamp 
[db] => Array ( 
    [value] => DateObject Object ( 
        [granularity] => Array ( 
            [0] => year 
            [1] => month 
            [2] => day 
            [3] => hour 
            [4] => minute 
            [5] => second 
            [6] => timezone ) 
        [errors] => Array ( ) 
        [serializedTime:DateObject:private] => 2014-06-10T13:09:55+02:00 
        [serializedTimezone:DateObject:private] => Europe/Paris 
        [timeOnly] => 
        [dateOnly] => 
        [originalTime] => 2014-06-10T13:09:55+02:00 
        [date] => 2014-06-10 13:09:55 
        [timezone_type] => 3 
        [timezone] => Europe/Paris ) 
    [value2] => DateObject Object ( 
        [granularity] => Array ( 
            [0] => year 
            [1] => month 
            [2] => day 
            [3] => hour 
            [4] => minute 
            [5] => second 
            [6] => timezone ) 
        [errors] => Array ( ) 
        [serializedTime:DateObject:private] => 2014-06-10T13:09:55+02:00 
        [serializedTimezone:DateObject:private] => Europe/Paris 
        [timeOnly] => 
        [dateOnly] => 
        [originalTime] => 2014-06-10T13:09:55+02:00 
        [date] => 2014-06-10 13:09:55 
        [timezone_type] => 3 
        [timezone] => Europe/Paris ) ) 

)

So I updated the function to:

function actomodule_cron(){
  $nid = 58;
  $node = node_load($nid);
  $simpleDate = time();
  $bigDate = new DateObject('now');

  $node->field_data['und'][0]['value'] =  $simpleDate;

  $node->field_data['und'][0]['db']['value'] =  $node->field_data['und'][0]['db']['value2'] =  $bigDate;

  node_save($node);
}

but didin't work.

How can I edit this field?

  • What type of field is field_data? – Clive Jun 10 '14 at 14:35
  • 1
    @Clive Probably - this [date_type] => datestamp – Petro Popelyshko Jun 10 '14 at 14:39
  • The field type is: Date (Unix timestamp) – Shyghar Jun 10 '14 at 14:44
  • @Shyghar look at this -> drupal.stackexchange.com/questions/26948/… – Petro Popelyshko Jun 10 '14 at 14:45
  • 1
    @Shyghar I tried your first code on /devel/php, it works fine for me,nodes are updated perfectly,, there may be confusion with field name or your cron run :) – shrish Jun 10 '14 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.