8

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

Thanks!

5 Answers 5

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.

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

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());
5

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

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;
$wrapper->field_subscriptionplan->set($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
$wrapper->field_subscription_signup_date->set(time());
$wrapper->save();
2

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.

$product->field_course_event_date->set(array(
  '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
  • 1
    Don't you mean value and value2? Jun 30, 2014 at 12:07

Your Answer

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

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