This question already has an answer here:
I've following code to check whether the user is born before today's date(the user date of birth is stored in a variable $form_data['dob']
). The code for it is as below:
if(change_date_format_to_db($form_data['dob'], 'Y-m-d') > change_date_format_to_db(date('Y-m-d'), 'Y-m-d'))
$this->mValidator->push_error($errors_msgs['user_dob_greater'], 'dob');
function change_date_format_to_db( $date, $current_format, $seperator = "/" ) {
if ( empty($date) || empty($current_format) ) {
return "";
}
$current_format = strtoupper($current_format);
$format_array = explode("-", $current_format);
$date_values = explode($seperator, $date);
for ($i=0;$i<3;$i++) {
$date_data[ $format_array[$i] ] = $date_values[$i] ;
}
$mysql_date_format = $date_data["Y"]."-".$date_data["M"]."-".$date_data["D"];
return $mysql_date_format;
}
Now instead of checking the user's date before today's date I want the user should be minimum 18 years old on today's date. How should I do this with above code? Thanks in advance.
</div>
Keep it simple with DateTime Object
$birthday = DateTime::createFromFormat('Y/m/d', $form_data['dob']);
$diff = $birthday->diff(new DateTime());
// ^^ now
if ($diff->y < 18) {
echo 'this site requires +18yrs';
}