I'm saving some date field on the code both on node and profile entity. I'm success on saving it for nodes, but when I try to save for profile2 entity, using the same chunck of code, no data is saved.

I'm doing this to nodes (after creating a new node with entity API):

// Creating a node and an entity wrapper
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
// Saving the date field
$entity->field_date[LANGUAGE_NONE][0] = array(
   'value' => strtotime($date),
   'timezone' => date_default_timezone(),
   'timezone_db' => date_default_timezone(),
   'date_type' => 'datestamp'
//Saving the node 

And almost the same way with profiles:

//Creating a profile2 'info' bundle     
$profile_info = profile2_create(array('type' => 'info', 'uid' => $user->uid));
// Trying to save the date field, same way as above
$profile_info->field_date[LANGUAGE_NONE][0] = array(
   'value' => strtotime($mydate),
   'timezone' => date_default_timezone(),
   'timezone_db' => date_default_timezone(),
   'date_type' => 'datestamp'
// Saving the profile

If someone knows what is happening I'd appreciate!


This might or might not fix your problem but since you're using entity metadata wrappers, you should use them fully:

$profile = profile2_create(...);
$profile_wrapper = entity_metadata_wrapper('profile2', $profile);
$profile_wrapper->field_date = array(...);

The above snippet can also be used with nodes, just replacing the creation & wrapper instantiation.

Also, if you're using a single timezone, you can safely omit it and just specify the actual timestamp value. You should probably omit the 'date_type' key too, that's inferred by the field config and could be causing issues.

In fact, you should just do: $profile_wrapper->field_date = 123456; // (timestamp)

Tested locally and it works!

You shouldn't use LANGUAGE_NONE or [0] indices because not only might it break i18n if you're translating fields but it's also not very robust.

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.