我有一个表单,其中的输入字段是这样的
<div class="row">
<?php echo $form->labelEx($model,'due_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'attribute'=>'due_date',
'model'=>$model,
'options' => array(
'mode'=>'focus',
'dateFormat'=>'d MM, yy',
'showAnim' => 'slideDown',
),
'htmlOptions'=>array('size'=>30,'class'=>'date'),
)
);
?>
<?php echo $form->error($model,'due_date'); ?>
</div>
我已经将这个表格保存在模型文件中。它是这样的
protected function beforeSave()
{
$this->due_date=date('Y-m-d', strtotime(str_replace(",", "", $this->due_date)));
return TRUE;
}
CJuiDatePicker 用于保存来自日期选择器的数据。它在保存时以 d mm yy 格式显示日期,但是当我要更新表格时,日期以 yy MM d 格式显示。如果我正在更改 beforeSave() 的日期格式,它正在存储日期格式为 0000-00-00 值。没有存储其他日期值。有人可以告诉我我哪里做错了吗?任何帮助和建议都将不胜感激。
最佳答案
试试这个:
protected function afterFind(){
parent::afterFind();
$this->due_date=date('d F, Y', strtotime(str_replace("-", "", $this->due_date)));
}
protected function beforeSave(){
if(parent::beforeSave()){
$this->due_date=date('Y-m-d', strtotime(str_replace(",", "", $this->due_date)));
return TRUE;
}
else return false;
}
将以上代码添加到您的模型中。它应该有效。
关于php - Yii 中的日期选择器将数据存储为 yy MM d 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311071/