I am trying to set the value of a date field to be 'all day' - but the format is being saved incorrectly.

The code where the value is being set:

 $kw = drupal_json_decode('[{"uid":3,"nid":55,"ts":[1395493200,1395838800,1396011600]}]');

    foreach ($kw as $id => $account) {
      $uid = $account['uid'];
      $user = user_load($uid);
      $nid = $account['nid'];
      $node = node_load($nid);

      // Create the Field Collection to attach to the event node.
      // KW is a contractor
      $fc_node = entity_create('field_collection_item', array('field_name' => 'field_add_kw'));
      $fc_node->setHostEntity('node', $node);
      $fc_node->field_kw_reference[LANGUAGE_NONE][]['uid'] = $uid;
      foreach ($account['ts'] as $key => $value) {
        $fc_node->field_kw_my_dates[LANGUAGE_NONE][]['value'] = format_date($value, 'custom', 'Y-m-d 00:00:00');

I am trying to force the H:i:s part to be 00:00:00. When I dpm() the outcome after saving $fc_node->save(); I see the following:

$fc->field_kw_my_dates['und'][0]['value'] gives : value (String, 19 characters ) 2014-03-26 00:00:00

This is correct. However, when I view the value of the date field on the node, it is being shown as:

Date: 26/03/2013 Time: 11.00

And not as an 'all day' entry.

How do I programmatically save a field with an all day date? Also is it possible to force the H:i:s part of the timestamp to be 00:00:00?


It appear if I use

format_date($value, 'custom', 'Y-m-d 13:00:00');

The 'all day' checkbox is selected. But this seems a bit hacky, what is the best way to achieve this instead of 'Y-m-d 13:00:00'?

  • Did you check timezone settings.. Is it causing above issue ? – Anil Sagar Mar 20 '14 at 10:34
  • The timezone settings appear to be set up correctly - I've also tried setting the value format_date($value, 'custom', 'Y-m-d 00:00:00', 'UTC'); but this doesn't seem to work either - just wont force H:i:s to 00:00:00 which I need – williamsowen Mar 20 '14 at 11:02
  • which hook you are using... – madhurjya Mar 20 '14 at 11:48
  • Hi - the function is above. This is being called from a hook_menu. Via ajax, a json object is being passed, for this example I've included it in the function – williamsowen Mar 20 '14 at 22:30
  • When you say your timesone settings seem to be set up correctly, what is it set to? Also check the timezone settings of your user if users are allowed to select their own. If it is in the database as midnight but displaying as 11 am that point to timezone issues. Also, what is the timezone handling configuration for the field if you check the field's setting page? – rooby Mar 21 '14 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.