0

From this answer, the result will be in text field field_cubagan.

Actually I want the result in another date field, but I choose text field instead as testing purposes because text field accept wide variety of result.

In below codes, the result will be 2012-06-30 10:05:00:

// Extract the first field item
$date_items = field_get_items('node', $node, 'field_date_start_rent');
$date_item = array_shift($date_items);
$integer_items = field_get_items('node', $node, 'field_subscription_period');
$integer_item = array_shift($integer_items);

// Get the field value
$start_rent = $date_item['value'];
$period = $integer_item['value'];

// Update the other field
$node->field_cubagan[$node->language][0]['value'] = $start_rent;

// Save the node
node_save($node);

But under Update the other field, instead of get unmodified result from $start_rent, I want the result become: "Datetime" + "x years" which "Datetime" is from field_date_start_rent while the "x" of the years is taken from field_subscription_period

I'm not sure if strtotime only works on unix timestamp or only on Y-m-d h:i:s format or works on both.

both below codes giving same result 2012-06-30 10:05:00 because in second codes, it converted to unix timestamp, and then converted back to the time format:

$node->field_cubagan[$node->language][0]['value'] = $start_rent;
$node->field_cubagan[$node->language][0]['value'] = date('Y-m-d h:i:s', strtotime($start_rent));

below codes will result in unix timestamp 1341050700:

$node->field_cubagan[$node->language][0]['value'] = strtotime($start_rent);

But strange thing is, I always failed when trying to calculate.

In below 3 codes is example where I always get warning and not works (I'm using +1 year as testing purposes):

$node->field_cubagan[$node->language][0]['value'] = strtotime('+1 year',$start_rent);
$node->field_cubagan[$node->language][0]['value'] = strtotime('+1 year',date('Y-m-d h:i:s', strtotime($start_rent)));
$node->field_cubagan[$node->language][0]['value'] = strtotime('+1 year',"date('Y-m-d h:i:s', strtotime($start_rent))");

What actually I've missed and what is the right codes?

1

I got it. It should be:

// Extract the first field item
$date_items = field_get_items('node', $node, 'field_date_start_rent');
$date_item = array_shift($date_items);
$integer_items = field_get_items('node', $node, 'field_subscription_period');
$integer_item = array_shift($integer_items);

// Get the field value
$start_rent = $date_item['value'];
$period = $integer_item['value'];

// Update the other field
$node->field_cubagan[$node->language][0]['value'] = date('Y-m-d h:i:s', strtotime("+$period year",strtotime($start_rent)));

// Save the node
node_save($node);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.