PHP date validation

PHP date validation FAQ: How do I validate a date in PHP?

This is a little bit of a tough question because you'll probably want more than the basic PHP date validation technique. For instance, to check a person's age you'll probably want to include a limited range of years, and to check something like a credit card expiration date, you'll want to make sure the date is in the future.

That being said, the basic PHP date validation technique is to use the checkdate function, like this:

$valid = checkdate($month, $day, $year);

The PHP checkdate function returns true if:

  1. $month is between 1 and 12,
  2. $year is between 1 and 32767, and
  3. $day is between 1 and the correct maximum number of days for the given month and year (correctly handling leap years).

Used in a PHP if/then statement, the PHP checkdate function call looks like this:

if (checkdate($month, $day, $year)) {
  // do something
}

PHP date validation - Summary

I hope this basic PHP date validation technique is helpful. As mentioned, you'll probably want to add additional date validation checks to your algorithm, but this is the basic check to validate a date in PHP.