新建index.php文件,实现表单的设计,如下图所示:
<form name="Myform" method="post" action="date.php">
<div align="center">
<h2>年龄计算器</h2>
出生年份:<input type="text" name="year"><br>
出生月份:<input type="text" name="month"><br>
出生天数:<input type="text" name="day"><br>
<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value="重置">
</div>
</form>
然后创建一个date.php,用于计算年龄,如图所示:
<?php
if(isset($_POST['submit'])&&$_POST['submit'])
{
if($_POST['year']!=""&&$_POST['month']!=""&&$_POST['day']!="")
{
$year=$_POST['year'];
$month=$_POST['month'];
$day=$_POST['day'];
@$birthday=mktime(0,0,0,$month,$day,$year);//把年月日转换成时间戳毫秒数
@$nowWindows=time();//获取现在的时间戳毫秒数
$age=$nowWindows-$birthday;
$age=floor($age/(365*24*60*60));
echo "<script>alert('您的年龄为:".$age."')</script>";
}
else
{
echo "<script>alert('请按要求输入日期');</script>";
$url="index.php";
echo "<script language='javascript' text='text/css'>";
echo "window.location.href='$url'";//跳转到index.php所指的页面
echo "</script>";
}
}