当前位置:Gxlcms > PHP教程 > 根据出生日期计算年龄(考虑到闰年的情况)

根据出生日期计算年龄(考虑到闰年的情况)

时间:2021-07-01 10:21:17 帮助过:59人阅读

主要考虑到闰年的情况,如果有人出生在2.29,那么不是闰年则过了2.28将算上一岁




  1. function age($birth) {
  2. $age = array();
  3. //$now = date('Ymd');
  4. $now = "20110228";
  5. //分解当前日期为年月日
  6. $nowyear = (int) ($now / 10000);
  7. $nowmonth = (int) (($now % 10000) / 100);
  8. $nowday = $now % 100;
  9. //分解出生日期为年月日
  10. $birthyear = (int) ($birth / 10000);
  11. $birthmonth = (int) (($birth % 10000) / 100);
  12. $birthday = $birth % 100;
  13. $year = $nowyear - $birthyear;
  14. if ($birthmonth>$nowmonth){
  15. $year--;
  16. }else if($birthmonth==$nowmonth){
  17. if($birthday==29&&$birthmonth=2){
  18. /* if($nowyear>3200||($nowyear%3200==0&&$nowyear%172800==0)){
  19. if($birthday>$nowday){
  20. $year--;
  21. }
  22. }else if($nowyear==3200){
  23. if (($birthday>$nowday)&&$nowday!=28){
  24. $year--;
  25. }
  26. }else */
  27. if ($nowyear%400==0||(($nowyear%4==0)&&($nowyear%100!=0))){
  28. if($birthday>$nowday){
  29. $year--;
  30. }
  31. }
  32. }
  33. }
  34. return $year;
  35. }

人气教程排行