如何在Codeigneter中验证出生日期应小于18岁


How to validate date of birth that should be less than 18 years old in Codeigneter

我用了很多东西,但我无法验证出生日期应该小于18岁

<?php echo form_error('DOB'); ?>
<div class="form-group">
<label class="col-sm-3 control-label" for="form-field-3" >
  Date Of Birth
</label>
<div class="col-sm-8">
<input type="text"  class="form-control" id="datepicker" name="DOB" placeholder="Date Of Birth" value="<?php echo set_value('DOB'); ?>"  >

或者,您可以为此使用回调。示例:

public function your_form_page()
{
    $this->load->library('form_validation');
    // this part is when you set your validation rules
    $this->form_validation->set_rules('DOM', 'Date of Birth', 'trim|required||callback_validate_age');
    $this->form_validation->set_message('validate_age','Member is not valid!');
}
// you need to add this on the controller, this will be the custom validation
public function validate_age($age) {
    $dob = new DateTime($age);
    $now = new DateTime();
    return ($now->diff($dob)->y > 18) ? false : true;
}