php 验证年龄是否超过 18 岁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1812589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate if age is over 18 years old
提问by Newb
Just wondering, can I do this to validate that a user has entered a date over 18?
只是想知道,我可以这样做来验证用户输入的日期是否超过 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');
}
}
Just stumbled across this function date_diff: http://www.php.net/manual/en/function.date-diff.phpLooks, even more promising.
刚刚偶然发现这个函数date_diff:http://www.php.net/manual/en/function.date-diff.php 看起来,更有前途。
回答by mauris
Why not? The only problem to me, is the User Interface - how you send out the error message elegantly to the user.
为什么不?对我来说唯一的问题是用户界面 - 如何优雅地向用户发送错误消息。
On another note, your function might not work properly as you did not intake a proper birthday (you are using a fixed birthday). You should change 'March 23, 1988' to $then
另一方面,您的函数可能无法正常工作,因为您没有获得正确的生日(您使用的是固定生日)。您应该将“1988 年 3 月 23 日”更改为 $then
//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');
}
}
Or you can:
或者你可以:
// 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;
}
回答by zeeshan
Here is a simplified extract from what I used for a banking system in Toronto, and this always worked perfectly, taking account of leap years of 366 days.
这是我在多伦多银行系统中使用的简化摘录,考虑到闰年 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;
}
回答by nonybrighto
I think it is best using the DateTime class for this.
我认为最好为此使用 DateTime 类。
$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";
}
DateTime::diff can also be used to compare the date with the current date.
DateTime::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: 1) for the second method , if $bday is greater than $today by 18 years or more, it will return older , so make sure date entered is less than $today . 2) DateTime works on php 5.2.0 and above
N/B: 1) 对于第二种方法,如果 $bday 比 $today 大 18 年或更长时间,它将返回旧的,因此请确保输入的日期小于 $today 。2) DateTime 适用于 php 5.2.0 及以上
回答by mtvee
if( strtotime("1988/03/23") < (time() - (18 * 60 * 60 * 24 * 365))) {
print "yes";
} else {
print "no";
}
...not accounting for leaps years however
...然而,不考虑闰年
回答by Nikita Prus
HTML Input:
HTML 输入:
<input type="date" class="form-control" placeholder="Data of Birth" name="dateOfBirth">
PHP code:
PHP代码:
function validateDateOfBirth($birthDay)
{
// convert user input date to string and +18 years;
// compare user input date with current date;
if (time() < strtotime('+18 years', strtotime($birthDay))) {
return 'Not 18';
}
return "user is older than 18 years old";
}
回答by Raijoe
Php file
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 code:
HTML代码:
<form action="" method="POST">
<p><label>Birth Date : </label>
<input id="bdate" type="date" name="bdate" required placeholder="" /></p>
<input class="btn register" type="submit" name="submit" value="Register" />
</form>

