doulu3399 2016-03-23 22:17
浏览 62

如何使用getdate()来验证用户的年龄?

When a user attempts to register with my site, I need to verify that they are old enough. I am trying to do this using the getdate() function.

I understand what getdate() does, but I am struggling to understand how to use it correctly for this purpose.

<?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>";
}
?>
  • 写回答

2条回答 默认 最新

  • dongou4052 2016-03-23 22:25
    关注

    Try:

    $registration = new DateTime(implode('-', array($yob, $mob, $dob)));
    $now = new DateTime();
    
    var_dump($now->diff($registration)->y);
    

    This will give you the actual age, taking months, days and leap years into account.

    DateTime Class Manual

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号