tags:

views:

77

answers:

4

In my form I am using drop down for showing day, month, year. can you help me to validate the date. I have to validate the entered age is greater than 15. also checking leap year

+6  A: 

The PHP function checkdate is what you're looking for.

Jacob Relkin
A: 
checkdate()

Age is not too hard too:

$validdate = (date("Y")-15).date("-m-d");
$bday = "$y-$m-$d";
if ($bday > $validdate) echo "underage!";
Col. Shrapnel
Thanks to everybody for your valuable comments.
php learner
A: 

you have to first convert your input date in yyyy-mm-dd frormate.you can not directly put validatation. you have to convert your date into timestamp and then after you have to check validation.

$strSystemMaxDate = (date('Y') - 15).'/'.date('m/d');
if(strtotime($strDateOfBirth) > strtotime($strSystemMaxDate))
{
    $arrErrors[] = _("Minimum age is 15 years.");
    $blnValidated = false;
}
why to use timestamp to compare? strings can be compared as well
Col. Shrapnel
A: 
<?php
    $yy = $_POST[ "yy" ];
    $mm = $_POST[ "mm" ];
    $dd = $_POST[ "dd" ];
    if ( checkdate( $mm, $dd, $yy ) === false )
    {
        die( "Invalid date" );
    }
    $birthDate = mktime( 0, 0, 0, $mm, $dd, $yy );
    $fifteenYearsAgo = strtotime( "-15 years" );
    if ( $birthDate > $fifteenYearsAgo )
    {
        die( "You're underage!" );
    }
?>
Salman A