Php 日期的格式无效
我有这样一个数组:Php 日期的格式无效,php,mysql,date,yii,Php,Mysql,Date,Yii,我有这样一个数组: 当我保存在mysql数据库中时 $model = new Holiday(); $model->holiday_date = $date; $model->display_name = $name; $model->save(); 当我写作时 值已成功保存,但未保存“false”数据 当看到验证错误大于错误时: Array ( [holiday_date] => Array ( [0] => The format of Ho
当我保存在mysql数据库中时
$model = new Holiday();
$model->holiday_date = $date;
$model->display_name = $name;
$model->save();
当我写作时
值已成功保存,但未保存“false”数据
当看到验证错误大于错误时:
Array ( [holiday_date] => Array ( [0] => The format of Holiday Date is invalid.))
我们使用
protected function beforeSave(){
if(parent::beforeSave()){
$this->holiday_date=date('Y-m-d',strtotime($this->holiday_date));
return TRUE;
}
return false;
}
您应该在beforeValidate上更改日期格式,或者更改
holiday\u date
属性的验证规则,因为beforeSave是在验证通过后执行的
public function beforeValidate()
{
if ($this->isNewRecord)
{
$this->holiday_date = date('Y-m-d', strtotime($this->holiday_date));
}
return parent::beforeValidate();
}
您的保存字段是char将其更改为dateSir,请提供更多详细信息。在mysql中,将字段类型从char更改为dateSir,mysql中已存在日期格式,如果提供了DO,请不要发送数据,或者将默认值设置为00-00-00000谢谢您的回答。当我使用验证规则保存日期格式时,但在这种情况下没有保存数据。您可以发布您模型的验证规则吗?是的,我们可以在Yii框架中创建自己的验证规则。我要求为您的属性“holiday_date”提供验证规则。无论如何,如果你已经解决了这个问题,你应该发布一个答案并接受它,这样问题就结束了。什么类型的$this->holiday\u date?日期时间还是字符串?
protected function beforeSave(){
if(parent::beforeSave()){
$this->holiday_date=date('Y-m-d',strtotime($this->holiday_date));
return TRUE;
}
return false;
}
public function beforeValidate()
{
if ($this->isNewRecord)
{
$this->holiday_date = date('Y-m-d', strtotime($this->holiday_date));
}
return parent::beforeValidate();
}