I am using Yii framework, set some rules for model validation when data is being modified. One of them which is to compare the valid DOB input. Criteria would be DOB should not be
- Greater than today
- Between 100 years as from today.
This is how I implement the rule:
/**
* Checks the valid input for DOB
*
* @return boolean
* @throws Error Message
*/
public function validDOB($attribute,$params){
if (($bdate = $this->bdate) != null){
$now = new DateTime("now");
$dob = DateTime::createFromFormat('d/m/Y', $bdate);
$interval = $now->diff($dob);
$curdate=strtotime($now->format('d/m/Y'));
$largedate=strtotime($dob->format('d/m/Y'));
if ($interval->y > 100){
$this->addError('bdate', 'Wow! Are you sure you are ' . $interval->y .' years old?');
}elseif ($curdate > $largedate){
$this->addError('bdate', 'Psst.. Hey... You are not a time traveller...');
}
}
}
So the problem is, the validator suppose to valid and throws me error when an invalid date is selected, but, say that today's date is 03/09/2014 (d/m/Y) and the selected date is 04/09/2014 (d/m/Y) suppose the system should prompt the error message but instead it returns as a valid input. I would have to select dates until about 13/09/2014 (d/m/Y) then only the system shows invalid input...
Same goes to year selection. Suppose that date is 13/09/2014 (d/m/Y), so between 100 years which means that if a user selects anything before 13/09/1914 (d/m/Y) the system would prompt invalid... But it also causes a bug like above..
Additionally I also realized that when I use strtotime() to compare future dates, it somehow causes those years between 100 year became invalid as well...
What have I done wrong?