0

Possible Duplicate:
how to validate date with PHP

Hi there,

I have a script which checks the date passed to it in this format YYYY-MM-DD and validates it, however it seems to be invalidating users who are under 20 whereas I'd prefer it to be under 18...I am not too familiar with ereg...could you please advise me on how to change this script to validate over 18's?

function valid_date( $fecha )
{
    // VALID FORMAT = yyyy-mm-dd
    if (@ereg ("([0-9]{4})-([0-9]{2})-([0-9]{2})", $fecha, $fecha_array))
    {
        // VALID DATE IN CALENDAR
    return ( checkdate($fecha_array[2],$fecha_array[3],$fecha_array[1]) )
    ? true
    : false ;
    }
    return false;
}

Thanks.

Community
  • 1
  • 1
ae2011
  • 69
  • 1
  • 1
  • 5

1 Answers1

2
<?php
//users birthday, ideally you would take this data from a form
$user_bd = strtotime('1954-06-05');

//Age requirement, using 18 as the minimum age required
$age_req = strtotime('+18 years', $user_bd);

//Grab the current UNIX timestamp and compare it to the minimum required
if(time() < $age_req){
    echo 'Not 18 years or older.';
}
else{
    echo 'Word. Come on in.';
}
?>
user457015
  • 960
  • 4
  • 14
  • 33