CakePHP日期选择的默认值

Cam*_*ron 5 php cakephp cakephp-2.0

我有一组选择出生日期:

<?php echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
                                    , 'dateFormat' => 'DMY'
                                    , 'minYear' => date('Y') - 100
                                    , 'maxYear' => date('Y') - 13)); ?>
Run Code Online (Sandbox Code Playgroud)

并希望在选择中将默认值设置为"DAY MONTH YEAR".

我已设法通过以下方式执行此操作:

<?php echo $this->Form->input('Profile.gender', array('label' => 'Gender', 'type' => 'select',
         'options' => array('Male'=>'Male','Female'=>'Female'),'empty'=>'Select Sex')); ?>
Run Code Online (Sandbox Code Playgroud)

但我不知道如何通过自动日期输入来做到这一点......

有人可以帮忙吗?谢谢

Ros*_*oss 13

只需添加:

'selected'=>date('Y-m-d')

到您的选项数组.

该示例将显示当前日期.如果需要静态日期,请根据需要进行更换.例如:

'selected'=>'2011-12-10'

显然,对于日期和时间,使用:

'selected'=>date('Y-m-d H:i:s')

要么

'selected'=>'2011-12-10 11:13:45'


小智 8

这种方式有效:

<?php 
echo $this->Form->input(
    'Profile.dob', 
    array(
        'label'         => 'Date of Birth',
        'dateFormat'    => 'DMY',
        'minYear'       => date('Y') - 100,
        'maxYear'       => date('Y') - 13,
        'empty'         => array(
            'day'       => 'DAY',
            'month'     => 'MONTH',
            'year'      => 'YEAR'
            )
        )
    ); 
?>
Run Code Online (Sandbox Code Playgroud)


Mo3*_*o3z 2

如果你不介意多写两行,你可以尝试这样做吗?

<?php
echo $this->Form->year('Profile.dob', date('Y') - 100, date('Y') - 13, array('empty' => "YEAR"));
echo $this->Form->month('Profile.dob', array('empty' => "MONTH"));
echo $this->Form->day('Profile.dob', array('empty' => 'DAY'));

?>
Run Code Online (Sandbox Code Playgroud)