2011-03-31 100 views
0

可能重复:
how to validate date with PHP在PHP中验证日期

您好,

我有一个脚本,检查传递给它的日期格式YYYY-MM- DD并验证它,但是它似乎使20岁以下的用户无效,而我更喜欢它不到18岁......我不太熟悉ereg ......请问如何改变这个脚本到v alidate超过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; 
} 

谢谢。

+1

有吨的类似的问题,可能重复[如何验证最新与PHP(http://stackoverflow.com/questions/4978872/how-to-validate-date-with-php) – 2011-03-31 09:14:36

+4

在'ereg'之前使用'@'并不会使其不再被弃用。 – Gordon 2011-03-31 09:28:44

+1

这段代码,不验证年龄! – user457015 2011-03-31 09:45:01

回答

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.'; 
} 
?>