0

I want to display the datetime field on my Drupal 8 form with some date loaded from the database, but the front end always shows empty fields as if no data has been provided. I tried several combinations to default_value field, but none of them are showing any thing.

The code I am using is the following.

$form['dateopening'] = array(
  '#type' => 'datetime',
  '#title' => t('Opening Date'),
  '#default_value'=> date('Y-m-d H:i:s', strtotime($dateFromDB)),
);

Why doesn't it set the default value as expected?

  • add a format like : $format = 'Y-n-d'; then add default value like: '#default_value' => date('c'), – harsh_behl_0007 Feb 8 '16 at 13:16
  • It is 'datetime' field not 'date' field which I wanted. It is not working. – suneel Feb 11 '16 at 5:23
0

The response is in this thread : on stackoverflow

$form['valid_dt'] = array(
    '#type' => 'datetime',
    '#default_value' => DrupalDateTime::createFromTimestamp($data->valid_dt),
    '#title' => $this->t('Valid'),      
);  
|improve this answer|||||

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.