Drupal - Setting a date field via entitity_metadata_wrapper

I was able to get this to work in a roundabout way. I may have been able to accomplish the same thing with time(), but this read better for me as I could follow exactly what I was doing if I ever came back to the code in the future.

// Update the Acceptance Date of Current Agreement.
$acceptance_date = new DateTime(date('Y-m-d 00:00:00'));
$company_wrapper->field_acceptance_date->set($acceptance_date->getTimestamp());

I know this question was asked some time ago, but for those who come across this question, the answer appears to be given at http://drupal.org/node/1803286. There is no setter callback for date fields in the Entity API, so you cannot set date values using entity_metadata_wrapper. The d.o node referenced in this answer gives a workaround.


If the field does not collect end date set it with timestamp:

$entity_wrapper->field_date = 1431670535;

If it does:

$entity_wrapper->field_date = array(
  'value' => '2015-05-12 11:30:38',
  'value2' => '2015-05-25 21:45:33',
);

Tags:

Entities

7