年龄计算器php实现

新建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>";
	}
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值