I am trying to set a field of date type through usage of entity_metadata_wrapper but the although the field is created as datetime in mysql, and creating a node sets it with expected YYY-MM-DD HH:MM:SS format, when using entity_metadata_wrapper it fails. The only thing I have managed to insert is timestamps, I have tried both Iso date format and the standard one.

Here is the line I am trying to use to update:

   // sets reminder date in db
   $wrapper->field_event_reminder_date->set($reminder_date->format("Y-m-d H:i:s"));


5 Answers 5


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.

  • Nice catch! -> Bookmarked for next time I have to play with date fields. Thanks friend. Commented Dec 12, 2012 at 22:22

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'));

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',
  • This is most useful for showing how to set dates with no end date.
    – reedbert
    Commented Dec 29, 2016 at 3:04

The examples posted did not work for me. I had a date field setup on the user entity, with only a start date and no time. The widget should not matter, but was just using the default text field.

Finally got it working using the entity metadata wrapper and only needed to use ->set(time()). The code below shows setting a checkbox "boolean" field, a product reference field and the date field.

$wrapper = entity_metadata_wrapper('user', $account);
$wrapper->field_subscription_active = 1;

It appears that how the value is set depends on what type of date field it is. This worked for me when using a Date field type, that had a start and end date.

  'value' => "2013-12-09 00:00:00",
  'value2' => "2013-12-10 00:00:00",

Using a different example, this worked for me with a Date field, unlimited value, start time only:

$invitation->field_dates = array(1431670535);
  • 1
    Don't you mean value and value2? Commented Jun 30, 2014 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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