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


I want to display datetime field on my Drupal 8 form with some date loaded from db. 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. Please help.

(If the post is a duplicate one, redirect me. I cant get what I wanted in search results)

1

1 Answers

1
votes

You have to set the default_value as DateTime object, like this:

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