为什么不?对我来说唯一的问题是用户界面 – 如何优雅地向用户发送错误消息.
另一方面,你的功能可能无法正常工作,因为你没有过正常的生日(你使用固定的生日).你应该将’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;
}