我想在Yii编写一个验证规则,但无法获得日期比较工作。如果End
不大于过去Start
的30天,则想要显示错误消息。日期加上30天验证
public function checkDate($attribute,$params)
{
$duration = strtotime(date('Y-m-d', strtotime($this->Start)) . ' +30 days');
if ($this->End < $duration)
$this->addError('End','30 days or more!');
}
有了这个每个输入产生的错误信息。我使用的是MySQL数据库,并开始和结束都是date
格式
编辑: 下面给出我已经试过
这样,即使现在不显示页面中的答案输入。
$startDate = $this->Start;
$endDate = $this->End;
$interval = $startDate->diff($endDate);
if ($interval < 30)
$this->addError('End','30 days or more!');
和
这给出了误差的100%的时间。
$duration = date('Y-m-d', strtotime($this->Start . '+ 30 days'));
if ($this->End < $duration)
$this->addError('End','30 days or more!');
小心使用strtotime来验证e用户输入见[strtotime Php手册](http://php.net/manual/en/function.strtotime.php)'时间戳的有效范围通常是从1905年12月13日星期五20:45:54 UTC到星期二,2038年1月19日03:14:07 UTC.'如果你可以使用php [日期时间](http://www.php.net/manual/en/class.datetime.php) – Henesnarfel 2012-02-06 18:47:10
介意解释为什么和/或给出选择? – enfield 2012-02-06 18:47:56