3

I am creating a custom form with "date_popup" form fields.
The only issue I have is that the default_value for the date popup form field shows only the first digit of the year, not the whole date.

Can anyone help with this?

  • 3
    I can't find the issue for it right now but it used to be that you had to set the #default_value in a very specific format to get it to work...'#default_value' => date('Y-m-d', $date) – Clive Aug 28 '12 at 15:14
  • Thanks,Clive. Yes, it is set the right way. After debugging i've found it turns wrong after pre_render function called ctools_smth_pre_render.Currently don't remember it's name clearly. Possibly it conflicts with ctools module.Going to investigate further later. Currently it looks this way:$form['all']['start'] = array( '#type' => 'date_popup', '#size' => 30, '#title' => t('Begin at Date'), '#maxlength' => 30, '#date_format' => 'Y-M-d','#default_value' => date('Y-M-d', $date), ); – swindler Aug 28 '12 at 18:25
9

Clive was absolutely right: The default value MUST be set using the date format "Y-m-d", such as with date('Y-m-d', $date).

The difference is that I used "Y-M-d" instead of "Y-m-d" for #default_value.

  • What a pain! The original issue was here in case you're interested – Clive Aug 29 '12 at 7:44
1

For Drupal 8:

use Drupal\Core\Datetime\DrupalDateTime;

$form['mydatefield']['widget'][0]['value']['#default_value'] = DrupalDateTime::createFromTimestamp(strtotime($mydatestring));
  • great addition, works good in D8 – Justme Aug 17 '18 at 6:41

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.