Validating a Date

The function checkdate( ) returns true if a given month, day, and year form a valid Gregorian date:

checkdate function

This function isn't based on a timestamp and so can accept a larger range of dates: basically any dates in the years 1 to 32767. It automatically accounts for leap years.

checkdate syntax

boolean checkdate(integer month, integer day, integer year)

This function accepts three parameters:

  1. month is between 1 and 12

  2. day is within the allowed number of days for the given month

  3. year is between 1 and 32767

And returns TRUE if the date given is valid; otherwise returns FALSE.

checkdate examples

// Works for a wide range of dates
$valid = checkdate(1, 1, 1066);
    //output: true
$valid = checkdate(1, 1, 2929);
    //output: true

// Correctly identify bad dates
$valid = checkdate(13, 1, 1996);
    //output: false
$valid = checkdate(4, 31, 2001);
    //output: false

// Correctly handles leap years
$valid = checkdate(2, 29, 1996);
    //output: true

$valid = checkdate(2, 29, 2001);
    //output: false