I'm working on date validation for a validation library. A date format and a variable containing the date to validate will be supplied to the library.
$validator->validate( $_POST['date'], 'm/d/y' );
It's pretty easy to do basic validation with DateTime::createFromFormat()
if ( DateTime::createFromFormat( $format, $date ) !== false ) {
// "valid" date
}
My issue is that with this method invalid dates such as 23/23/23 pass because the DateTime
class will shift the date and set it to 2024-11-23. I know of the existence of checkdate()
but without knowing the format I can't see a way to extract the origin month/day/year.
Any ideas on how to validate a date like this?