【问题标题】:Validating date in PHP [duplicate]在 PHP 中验证日期 [重复]
【发布时间】:2011-03-31 09:10:01
【问题描述】:

可能重复:
how to validate date with 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;
}

谢谢。

【问题讨论】:

标签: php date ereg


【解决方案1】:
<?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.';
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2012-08-15
    • 2018-08-16
    相关资源
    最近更新 更多