0

可能重复:
如何使用 PHP 验证日期

你好呀,

我有一个脚本,它检查以 YYYY-MM-DD 格式传递给它的日期并对其进行验证,但是它似乎使 20 岁以下的用户无效,而我希望它低于 18 岁......我不是对 ereg 太熟悉了...您能告诉我如何更改此脚本以验证 18 岁以上的人吗?

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;
}

谢谢。

4

1 回答 1

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.';
}
?>
于 2011-03-31T09:50:21.820 回答