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?

2
  • 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
    Commented Aug 28, 2012 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
    Commented Aug 28, 2012 at 18:25

2 Answers 2

10

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.

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

For Drupal 8:

use Drupal\Core\Datetime\DrupalDateTime;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.