Book HomePHP CookbookSearch this book

3.9. Validating a Date

3.9.1. Problem

You want to check if a date is valid. For example, you want to make sure a user hasn't provided a birthdate such as February 30, 1962.

3.9.2. Solution

Use checkdate( ):

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

3.9.3. Discussion

The function checkdate( ) returns true if $month is between 1 and 12, $year is between 1 and 32767, and $day is between 1 and the correct maximum number of days for $month and $year. Leap years are correctly handled by checkdate( ), and dates are rendered using the Gregorian calendar.

Because checkdate( ) has such a broad range of valid years, you should do additional validation on user input if, for example, you're expecting a valid birthdate. The Guinness Book of World Records says the oldest person ever reached 122. To check that a birthdate indicates a user between 18 and 122 years old, use the pc_checkbirthdate( ) function shown in Example 3-1.

Example 3-1. pc_checkbirthdate( )

function pc_checkbirthdate($month,$day,$year) {
    $min_age = 18;
    $max_age = 122;

    if (! checkdate($month,$day,$year)) {
        return false;
    }

    list($this_year,$this_month,$this_day) = explode(',',date('Y,m,d'));

    $min_year = $this_year - $max_age;
    $max_year = $this_year - $min_age;

    print "$min_year,$max_year,$month,$day,$year\n";
    
    if (($year > $min_year) && ($year < $max_year)) {
        return true;
    } elseif (($year == $max_year) && 
              (($month < $this_month) ||
               (($month == $this_month) && ($day <= $this_day)))) {
        return true;
    } elseif (($year == $min_year) &&
              (($month > $this_month) ||
               (($month == $this_month && ($day > $this_day))))) {
        return true;
    } else {
        return false;
    }
}

Here is some sample usage:

// check December 3, 1974
if (pc_checkbirthdate(12,3,1974)) {
    print "You may use this web site.";
} else {
    print "You are too young to proceed.";
    exit( );
}

This function first uses checkdate( ) to make sure that $month, $day, and $year represent a valid date. Various comparisons then make sure that the supplied date is in the range set by $min_age and $max_age.

If $year is noninclusively between $min_year and $max_year, the date is definitely within the range, and the function returns true. If not, some additional checks are required. If $year equals $max_year (e.g., in 2002, $year is 1984), $month must be before the current month. If $month equals the current month, $day must be before or equal to the current day. If $year equals $min_year (e.g., in 2002, $year is 1880), $month must be after the current month. If $month equals the current month, $day must be after the current day. If none of these conditions are met, the supplied date is outside the appropriate range, and the function returns false.

The function returns true if the supplied date is exactly $min_age years before the current date, but false if the supplied date is exactly $max_age years after the current date. That is, it would let you through on your 18th birthday, but not on your 123rd.

3.9.4. See Also

Documentation on checkdate( ) at http://www.php.net/checkdate; information about The Guinness Book's oldest person is at http://www.guinnessworldrecords.com (navigate to "The Human Body," "Age and Youth," and then "Oldest Woman Ever").



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.