本文实例讲述了php简单计算年龄的方法。分享给大家供大家参考,具体如下:
/**
* $date是时间戳
* $type为1的时候是虚岁,2的时候是周岁
*/
function getagebybirth($date,$type = 1){
$nowyear = date("y",time());
$nowmonth = date("m",time());
$nowday = date("d",time());
$birthyear = date("y",$date);
$birthmonth = date("m",$date);
$birthday = date("d",$date);
if($type == 1){
$age = $nowyear - ($birthyear - 1);
}else{$type == 2}{
if($nowmonth<$birthmonth){
$age = $nowyear - $birthyear - 1;
}elseif($nowmonth==$birthmonth){
if($nowday<$birthday){
$age = $nowyear - $birthyear - 1;
}else{
$age = $nowyear - $birthyear;
}
}else{
$age = $nowyear - $birthyear;
}
}
return $age;
}
ps:本站还提供了一个unix时间戳转换工具,包含了各种常见语言针对时间戳的操作方法,提供给大家参考:
unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
更多关于php相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《php数学运算技巧总结》、《php数组(array)操作技巧大全》、《php数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、《php运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!