I have an entity type with a field called "field_deadline" of type Date, where "Render as a regular field" is set to True. I need to set its format programmatically. I tried the following pieces of code that are ignored:

function hook_date_popup_process_alter(&$element, &$form_state, $context) {
  $element['#date_format'] = 'F j, Y';
}

Also, in a template function as follows:

  $elem = 'field_deadline';
  $lang = $form[$elem]['#language'];
  $form[$elem][$lang][0]['value']['#date_format'] = 'F j, Y';
  $form[$elem]['#date_format'] = 'F j, Y';

Please suggest an approach that works.

Comments

dibyajyoti.mallick’s picture

Here is a code for change date format:

$node->field_date[LANGUAGE_NONE][0] = array(
'value' => format_date(strtotime('now'), 'custom', 'Y-m-d H:i:s', 'UTC'),
'timezone' => 'UTC',
'timezone_db' => 'UTC',
);

Please try it and let me know if you have any doubt.

aibek76’s picture

Thanks, but your code hardcodes a value to be 'now', also a timezone. I need to format a date entered by user and display it accordingly.