当用户尝试在我的网站注册时,我需要验证他们是否足够老。我正在尝试使用getdate()函数来完成此操作。
我知道getdate()是做什么的,但我正在努力理解如何正确地使用它来达到这个目的。
<?php
$fn = $_POST["fullname"];
$un = $_POST["username"];
$pw = $_POST["password"];
$dob = $_POST["dayofbirth"];
$mob = $_POST["monthofbirth"];
$yob = $_POST["yearofbirth"];
$date = getdate();
if ( $yob =$yob>= $date["year"]-16)
{
echo "Too young to register!";
}
elseif ($yob <=1899)
{
echo "Don't be silly, you are not that old!";
}
else
{
echo "<h1>Thank you for registering with us!</h1>";
echo "<p> You have successfully registered with these details:
<br>Your full name :$fn<br> Username: $un
<br>Date of birth: $dob $mob $yob</p>";
}
?>发布于 2016-03-24 06:25:35
尝试:
$registration = new DateTime(implode('-', array($yob, $mob, $dob)));
$now = new DateTime();
var_dump($now->diff($registration)->y);这将为您提供实际年龄,并将月、日和闰年考虑在内。
DateTime Class Manual
https://stackoverflow.com/questions/36189759
复制相似问题