我想在保存之前进行验证,并检查日期是否存在+/- 30天。 它在“宽限期内”不断向我显示我自己的错误。我的理由是,如果我减去/添加30天到用户的输入并搜索它。如果它存在,那么它仍然在宽限期内。 我错过了什么?
$criteria = new CDbCriteria;
//subtract number of days to see if a record exist
$minusthirtyDays = date( 'Y-m-d',strtotime($this->effective_from_date ." -30 days"));
$addthirtyDays = date( 'Y-m-d',strtotime($this->effective_to_date ." +30 days"));
$criteria->compare('pid',$this->id1,'OR');
$criteria->compare('pid',$this->id2);
$criteria->addBetweenCondition('effective_from_date', $minusthirtyDays,$this->effective_to_date,'OR');
$criteria->addBetweenCondition('effective_to_date',$this->effective_to_date ,$addthirtyDays,'OR');
$model = self::model()->findall($criteria);
if (!empty($model)) {
$this->addError($attribute, "Date is within grace period of 30 days.");
}
答案 0 :(得分:0)
$minusthirtyDays = date( 'Y-m-d',strtotime($this->effective_from_date ." -30 days"));
$addthirtyDays = date( 'Y-m-d',strtotime($this->effective_to_date ." +30 days"));
必须有
$minusthirtyDays = date( 'Y-m-d',strtotime("-30 days", strtotime($this->effective_to_date));
$addthirtyDays = date( 'Y-m-d',strtotime("+30 days", strtotime($this->effective_to_date)));
请参阅strtotime功能