日期选择的CakePHP默认值

日期选择的CakePHP默认值,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,我有一套关于出生日期的选择: <?php echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth' , 'dateFormat' => 'DMY' , 'minYear' => date('Y') - 100

我有一套关于出生日期的选择:

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

并希望将默认值设置为选择中的单词“日-月-年”

我通过以下方式成功地实现了这一点:

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

但我不知道如何使用automagic日期输入来实现这一点

有人能帮忙吗?谢谢

只需添加:

'selected'=>日期('Y-m-d')

到您的选项数组

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

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

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

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


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

如果您不介意再写两行,您可以试试吗

<?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'));

?>

我在上面的cakephp 2.0中这样实现了它

echo $this->Form->dateTime('dob', 'DMY','', array(
    'value'=>'1987-02-12',
    'empty'=>false,
    'label'=>'Date Of Birth',
    'minYear'=>date('Y')-60,
    'maxYear'=>date('Y')-15)
);
在cakephp的2.0 api之后添加了“value”属性,并删除了“selected”

Cakephp手册说:$selected参数已从FormHelper中的几个方法中删除。所有方法现在都支持$attributes['value']键,应该使用它来代替$selected。此更改简化了FormHelper方法,减少了参数数量,并减少了$SELECT创建的重复。有效的方法有:

FormHelper::select()
FormHelper::dateTime()
FormHelper::year()
FormHelper::month()
FormHelper::day()
FormHelper::hour()
FormHelper::minute()
FormHelper::meridian()

如果相应的日期请求数据为空,则将Cakephp设置为空值,因此只需在回显日期输入字段之前将其设置为空:

$this->request->data['Profile']['dob'] = null;

echo $this->Form->month('Profile.dob', array('empty' => "MONTH"));
// do the same for year and day
空值(在您的情况下为“日期”、“月”、“年”字段)将在输入表单中预先选择

这种方式工作:

<?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'
            )
        )
    ); 
?>

这就是目前在cakephp 2.5中对我有效的方法:

echo $this->Form->input('fecha_pos_fijacion', array(
    'label' => 'Fecha de fijación',
    'dateFormat' => 'DMY',
    'minYear' => date('Y'),
    'maxYear' => date('Y')+5,
    'orderYear' => 'asc',
    'selected' => date('Y-m-1')
    )
))

这有点复杂,它会给你一个默认值设置为当前月份的第一天。年份的可能值介于当前和未来5年之间,以升序显示

更完整的是:

echo $this->Form->input('fecha_transporte', array(
                    'label' => '',
                    'dateFormat' => 'DMY',
                    'minYear' => date('Y'),
                    'maxYear' => date('Y')+5,
                    'orderYear' => 'asc',
                    'selected' => date('Y-m-1', strtotime("+30 days"))
                    )
            );

这里的默认值是下个月的第一天

另外,你真的确定你真正的意思吗

您似乎混淆了默认值和空值。如果使用“选定”将默认值设置为
日-月-年
,则代码将无法工作,因为
日-月-年
不是有效日期。 使用


看起来像是您正在寻找的,邀请用户输入日期。

实际单词中的“日-月-年”如何“selected”是我一直在寻找的关键字,谢谢分享:)您可以添加几行JS附加到文本的开头吗?除了修改表单helper之外,我想不出一种更“黑”的方法。给我这个错误:
致命错误:1816行的/Users/cameron/Sites/social/lib/Cake/View/helper/FormHelper.php中不支持的操作数类型
@cameron,可能是我缺少了一些大括号,或者你想试试$this->form->year(…)。@cameron,很抱歉,我本应该提到的。它只适用于cake 1.3+这是正确的答案,但在year()调用中,您应该这样做:
echo$This->Form->year('Profile.dob',array('minYear'=>date('Y')-100,'maxYear'=>date('Y')-13,'empty'=>“year”)不错!这正好回答了最初的问题(我也有这个问题),并且没有切换到三个单独的语句。
'empty' => array(
  'day' => 'DAY',
  'month' =>'MONTH',
  'year' =>'YEAR'
);