只是想知道,我可以这样做来验证用户输入了超过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: http://www.php.net/manual/en/function.date-diff.php 看起来,更有希望。
答案 0 :(得分:22)
为什么不呢?对我来说唯一的问题是用户界面 - 如何优雅地向用户发送错误消息。
另一方面,您的功能可能无法正常工作,因为您没有过正常的生日(您使用固定的生日)。你应该将'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');
}
}
或者你可以:
// 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;
}
答案 1 :(得分:14)
这是我用于多伦多银行系统的简化摘录,考虑到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;
}
答案 2 :(得分:4)
我认为最好使用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也可用于将日期与当前日期进行比较。
$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)对于第二种方法,如果$ bday大于$今年18年或更长时间,它将返回更旧,因此请确保输入的日期小于$今天
。 2)DateTime适用于php 5.2.0及以上版本
答案 3 :(得分:2)
if( strtotime("1988/03/23") < (time() - (18 * 60 * 60 * 24 * 365))) {
print "yes";
} else {
print "no";
}
......但是没有考虑到飞跃年数
答案 4 :(得分:0)
HTML输入:
<input type="date" class="form-control" placeholder="Data of Birth" name="dateOfBirth">
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";
}
答案 5 :(得分:-2)
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:
<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>