php简单年龄计算器案例

简介: php简单年龄计算器案例

什么是时间戳

时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。

cyg.php

<style>
body{
background:#ccc;
}
</style>
<form name="myForm" action="date.php" method="post"><!--提交代码到当前cyg.php的同级目录下date.php-->
年龄计算器
<br>
出生年份:<input type="text" value="" name="year"><br>
出生月份:<input type="text" value="" name="month"><br>
出生天数:<input type="text" value="" name="day"><br>
<input type="submit"><!--提交代码到dete.php中-->
<input type="reset"><!--重新输入,也就是重置-->
</form>

date.php

<?php
$year = $_POST['year'];//获取年
$month = $_POST['month'];//获取月份
$day = $_POST['day'];//获取日
$birthday = mktime(0,0,0,$month,$day,$year);//比如你输入的是2000年5月18日。那就是(北京时间)1970年01月01日08时00分00秒到2000年5月18日的总秒数.
$nowunix = time();//获取1970年01月01日08时00分00秒到当前时间的时间戳(秒数)
$age = $nowunix - $birthday;//当前时间的时间戳减去某个人生日的时间戳===某个人今年多少岁
$age = floor($age / (365*24*60*60));//然后是向下取整,$age / (365*24*60*60)根据这个算法得出,现在它多少岁了
echo "<script>alert('您的年龄为:".$age."')</script>";
?>

效果:


相关文章
php案例:$_ENV的数据怎么样才能显示出来.$_ENV的简单运用
php案例:$_ENV的数据怎么样才能显示出来.$_ENV的简单运用
php案例:$_ENV的数据怎么样才能显示出来.$_ENV的简单运用