By aibek76 on
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
Change date format programmatically
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.
Thanks, but your code
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.
use Token
https://drupal.stackexchange.com/questions/4930/how-to-programmatically-create-a-custom-token-in-a-module