-3

我想做一个简单的 PHP 脚本,这是我的主页。我希望用户输入他们的出生日期或从下拉日历中选择它,然后根据他们输入的内容我可以检查他们是否是 21 岁。如果不是,我将它们发送到一个页面,如果它们是,我将它们发送到另一个页面。这是我到目前为止所拥有的。

    <div id="age">
You must be 18 or older to view this site.<br>
  You must be 18 or older to view this site.<br>
    <form action="#" method="post">
    Please input your date of birth:
    <input type="text" name="month">
<input type="text" name="day">
<input type="text" name="year">
    <input type="submit" name="submit" value="Verify Age">
    </form>
</div>
        <p></p>

        <?php include "footer.php" ?>
<?php
$month = $_REQUEST['bmonth'];
$day = $_REQUEST['day'];
$year = $_REQUEST['year'];
$this_day = date(d);
$this_month = date(m);
$this_year = date(Y);
$day_val = $this_day - $day;
$month_val = $this_month - $month;
$year_val = $this_year - $year;
if (!empty($_POST['submit'])) {
    $today = date_create('today');
    $birth = date_create("{$_POST['year']}-{$_POST['month']}-{$_POST['day']}");
    $age = date_diff($today, $birth)->y;

    if ($age >= 18) {
        header('Location: home.php');
    } else { 
        header('Location: contact.php');
    }
}
?>
4

3 回答 3

0
<?php

$minAge = 18; // You might read this from a file/database.

$minAge *= 3600*24*365.25;  // $minAge in seconds



$html = <<< OET

  You must be 18 or older to view this site.

  <br />

  <form action="#" method="post">

    Please input your date of birth:

    <input type="text" name="dob" value="" />

    <br />

    <input type="submit" name="submit" value="Verify Age" />

  </form>

OET;



if(isset($_POST['submit'])){

    $birth_date = strtotime($_POST['dob']);

    $now = strtotime("now");

    $age = $now - $birth_date; // age is in seconds

    if($age > $minAge)

        echo "Over minimum age."; // You could use header here.

    else

        echo "Under minimum age."; // You could use header here.

} else

{

    echo $html;

}
于 2013-11-13T11:56:03.877 回答
0

试试这个:

if (!empty($_POST['submit'])) {
    $today = date_create('today');
    $birth = date_create("{$_POST['year']}-{$_POST['month']}-{$_POST['day']}");
    $age = date_diff($today, $birth)->y;

    if ($age >= 18) {
        header('Location: example1.php');
    } else { 
        header('Location: example.php');
    }
}
于 2013-11-13T12:04:00.933 回答
-1

这里的朋友:https ://gist.github.com/juniorb2ss/a21d5949a91503b9a417

任何问题都可以问;)

if(!empty($_POST))
{
 $d = $_POST['d'];
 $m = $_POST['m'];
 $y = $_POST['y'];
 $inicio = "$d/$m/$y";
 $fim = Date('d/m/Y', time());

 $b = DateTime::createFromFormat('d/m/Y', $inicio);     
 $e = DateTime::createFromFormat('d/m/Y', $fim);     
 $intervalo = $b->diff($e);
 $f = (array)$intervalo;

 if ($f['y'] >= 18) 
 {
     print $intervalo->format('You have %Y years');
     //header('Location: example1.php');
 } 
 else 
 { 
     print $intervalo->format('You have %Y years');
     //header('Location: example.php');
 }
}

通知:

You must be 18 or older to view this site.<br>
<form action="#" method="post">
Please input your date of birth:
<input type="text" size="2" name="d" value=""> 
/ <input size="2" type="text" name="m" value=""> 
/ <input size="2" type="text" name="y" value="">
<input type="submit" name="submit" value="Verify Age">
</form>
于 2013-11-13T12:16:11.113 回答