2009-11-28 6 views
8

사용자가 18 세 이상을 입력했음을 확인하기 위해이 작업을 수행 할 수 있습니까? , http://www.php.net/manual/en/function.date-diff.php 외모보다 유망한 : 연령이 18 세 이상인 경우 유효 함

//Validate for users over 18 only 
function time($then, $min) 
{ 
$then = strtotime('March 23, 1988'); 
//The age to be over, over +18 
$min = strtotime('+18 years', $then); 
echo $min; 
if(time() < $min) 
{ 
die('Not 18'); 
} 
} 

그냥이 기능 date_diff를 우연히 발견했다.

+1

이것이 최고의 보안을위한 것이 아닙니다. ;-) 성인용 콘텐츠에 액세스하려는 모든 어린이가 정확한 나이를 입력 할 것이라고 확신합니다. – scunliffe

+0

아니요, 저는 PHP에 대해 배우는 중입니다. 보안에 전혀 관심이 없을 것입니다. – Newb

답변

19

왜 안 되니? 유일한 문제는 사용자 인터페이스입니다. 사용자에게 오류 메시지를 우아하게 보내는 방법입니다.

또 다른 점은 생일을 맞이하지 않은 경우 (생일을 사용하는 경우) 기능이 제대로 작동하지 않을 수 있다는 것입니다. 당신은

//Validate for users over 18 only 
function validateAge($then, $min) 
{ 
    // $then will first be a string-date 
    $then = strtotime($then); 
    //The age to be over, over +18 
    $min = strtotime('+18 years', $then); 
    echo $min; 
    if(time() < $min) { 
     die('Not 18'); 
    } 
} 

다음 $에 '1988 년 3 월 23'으로 변경해야합니다 또는 당신은 할 수 있습니다

// validate birthday 
function validateAge($birthday, $age = 18) 
{ 
    // $birthday can be UNIX_TIMESTAMP or just a string-date. 
    if(is_string($birthday)) { 
     $birthday = strtotime($birthday); 
    } 

    // check 
    // 31536000 is the number of seconds in a 365 days year. 
    if(time() - $birthday < $age * 31536000) { 
     return false; 
    } 

    return true; 
} 
+0

난 그저 그걸하고 있었어. 편집자. : P – William

+0

하하 내가 더 빨랐다 고 생각하니? 내 잘못이야. – mauris

+8

또한 1 년은 기술적으로 365.242199 일이라는 것을 기억하십시오. 따라서 31536000 대신 31556926을 곱해야합니다. 또한 Google은 이러한 종류의 계산에 대해 정말 멋지 며 Google에 "1 초 만에 1 년"을 묻습니다. – William

2
if(strtotime("1988/03/23") < (time() - (18 * 60 * 60 * 24 * 365))) { 
    print "yes"; 
} else { 
    print "no"; 
} 

을 ... 도약 년 동안 차지하지 여기

+0

동일한 목표를 달성하는 데 너무나 많은 방법이 있습니다. – Newb

11

가 단순화 그러나 토론토의 은행 시스템에 사용했던 것에서 추출한 것이고, 이것은 366 일의 윤년을 고려하여 항상 완벽하게 작동했습니다.

/* $dob is date of birth in format 1980-02-21 or 21 Feb 1980 
* time() is current server unixtime 
* We convert $dob into unixtime, add 18 years, and check it against server's 
* current time to validate age of under 18 
*/ 

if (time() < strtotime('+18 years', strtotime($dob))) { 
    echo 'Client is under 18 years of age.'; 
    exit; 
} 
-1

PHP 파일

if (isset($_POST['bdate'])){ 
    $bdate = $_POST['bdate']; 
    $age = (date("Y-m-d") - $bdate); 



} //if age if 17 or younger error msg 
if ($age < 17) { 
    echo "Must 18 or older."; 
} 
else{ //if age is 120 or greather error msg 
    if ($age > 120) { 
     echo "Real age please."; 
    } 
    else{ 
     echo "$age"; 
    } 
} 

HTML 코드 :

<form action="" method="POST"> 

    <p><label>Birth Date &nbsp;&nbsp;: </label> 
    <input id="bdate" type="date" name="bdate" required placeholder="" /></p> 
    <input class="btn register" type="submit" name="submit" value="Register" /> 
</form> 
2

내가이의 날짜 시간 클래스를 사용하여 최고라고 생각합니다.

$bday = new DateTime("22-10-1993"); 
$bday->add(new DateInterval("P18Y")); //adds time interval of 18 years to bday 
//compare the added years to the current date 
if($bday < new DateTime()){ 
    echo "over 18"; 
}else{ 
    echo "below 18"; 
} 

날짜 시간 :: DIFF는 현재 날짜와 날짜를 비교하는 데 사용할 수 있습니다.

$today = new DateTime(date("Y-m-d")); 
$bday = new DateTime("22-10-1993"); 
$interval = $today->diff($bday); 
if(intval($interval->y) > 18){ 
    echo "older than 18"; 
}else{ 
    echo "younger than 18"; 
} 

N/B : $ bday 18 세 이상 오늘 $보다 큰 경우 1) 두 번째 방법를 들어, 나이가 반환, 그래서 입력 확인 날짜를 만들 것입니다은 $ 오늘 보다 작습니다. 2) DateTime은 PHP 5.2.0 이상에서 작동합니다.

관련 문제