Age based on date of birth (considering leap years)

Source: Internet
Author: User
Mainly considering the case of leap years, if someone was born in 2.29, then not a leap year then 2.28 will be counted




  1. function Age ($birth) {
  2. $age = Array ();
  3. $now = Date (' Ymd ');
  4. $now = "20110228";
  5. Decomposition current date is month day
  6. $nowyear = (int) ($now/10000);
  7. $nowmonth = (int) (($now% 10000)/100);
  8. $nowday = $now% 100;
  9. Decomposition date of birth is month day
  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. }
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.