Drupal 7 date popup default value is blank

2019-08-19 06:34发布

问题:

I am trying to get a default value in for the default value of a date_popup that exists within a fieldset and I have followed the other suggestions here but the value is always blank.

$format = 'm/d/Y';
$primary_start1 = null;
if(isset($vals["primary_start"])){
    if("-1" != $vals["primary_start"]){
        $primary_start1 = (int)$vals["primary_start"];

    }
}

 $form['dates']['primary']['primary_start'] = array(
    '#title' => t('Start date'),
    '#name' => 'primary_start',
    '#type' => 'date_popup',
    '#date_timezone' => FALSE,
    '#default_value' => date('m/d/Y',$primary_start1),
    '#date_format' => $format,
    '#required' => TRUE,
    '#date_label_position' => 'none',


);

The date comes in as a string, so I convert it to an int...which I know works because I dump the variable.

Am I doing anything wrong here?

Drupal v 7.22

Date Module - 7.x-2.6

Date Popup - 7.x-2.6

回答1:

You should use another date format:

$format = 'Y-m-d';

Also it's better to use drupal core function for date formatting:

format_date($primary_start1, 'custom', $format)