2

I've got a content type with fields: starting date: Date (Unix timestamp) duration: integer ending date: Date (Unix timestamp)

User is setting only starting date (year, month, day) and duration (in days) end after saving the content I would like to automatically calculate ending date (=starting date + duration)

I suppose I should use rules. I tried using actions like set value or calculate value but I failed. Could you help me?

1

Interval module may help you. It provides an interval field type and also a rules action: "Apply an interval to a date".

-1

For a quick fix I would recommend making a simple custom module with this function:

function YOUR_MODULE_node_presave($node) {
  if($node->type === "your_content_type") { // Replace with the content type of your node.
    // Replace $node->XXX with the actual names of your fields.
    $startdate = $node->start_date[LANGUAGE_NONE][0]['value'];
    $duration = $node->duration[LANGUAGE_NONE][0]['value'];
    $enddate = &$node->end_date[LANGUAGE_NONE][0]['value'];

    $enddate = strtotime('+ ' . $duration . ' days', $startdate);
  }
}

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.