Kohana 3.2出生日期验证


Kohana 3.2 Validation in Birthdate

我想做的是验证年龄是否小于18岁或大于33岁

public function validate($arr) {
    return Validation::factory($arr)
        ->rule('username', 'not_empty')
        ->rule('last_name', 'not_empty')
        ->rule('first_name', 'not_empty')
        ->rule('email', 'not_empty')
        ->rule('email', 'email_domain')
        ->rule('phone_num', 'not_empty')
        ->rule('birthdate', 'not_empty')
        ->rule( 'birthdate', array( $this, 'check_birthday' ) );     
}
public function check_birthday()
{
    check if age is not under 18 or over 33
   return boolean;
}

您可以使用Kohana_Date常量,如:

return ($date < (time() - 18 * Date::YEAR)) AND ($date > (time() - 33 * Date::YEAR));

return Valid::range($date, time() - 33 * Date::YEAR, time() - 18 * Date::YEAR);

如果需要,可以使用strtotime将日期格式转换为时间戳。