欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-04-05 23:02:01
...
主要考虑到闰年的情况,如果有人出生在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. }
复制代码