I want to make sure that the birthday entered is not in the future. If I input a future date it should show an error message.
public function birth($name, $value) {
$datePattern = '/^[0-9]{2}\/[1-9]{2}\/[1-9]{4}?$/';
$match = preg_match($datePattern, $value);
if ( $match === false ) {
$field->setErrorMessage('Error testing field.');
return;
}
if ( $match != 1 ) {
$field->setErrorMessage('Invalid date format.');
return;
}
$dateParts = explode('/', $value);// i try to see if i could take everything after / to form an array
$month = $dateParts[0];
$day = $dateParts[1];
$year = $dateParts[2];
$dateString = $month . $day . $year;
$exp = new \DateTime($dateString);
$now = new \DateTime();
if ( $exp < $now ) {
$field->setErrorMessage('Cant be a date after the current date');
return;
}
$field->clearErrorMessage();
}
However it's not working correctly. I can't compare $dateString
with DateTime