분류 php

PHP : 나이 계산

컨텐츠 정보

  • 조회 776 (작성일 )

본문

  1. <?php
  2. /*
  3. Calculate Age
  4.  
  5. With this function you can calculate the age of a person
  6.  
  7. Example:
  8. echo "Age is: " . birthday ("1984-07-05");
  9.  
  10. Result will be (23 Feb 2005) = "Age is: 20"
  11.  
  12. calculate years of age (input string: YYYY-MM-DD)
  13.  
  14. */
  15. function birthday ($birthday)
  16. {
  17. list($year,$month,$day) = explode("-",$birthday);
  18. $year_diff = date("Y") - $year;
  19. $month_diff = date("m") - $month;
  20. $day_diff = date("d") - $day;
  21. if ($month_diff < 0) $year_diff--;
  22. elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
  23. return $year_diff;
  24. }
  25.  
  26. echo birthday("1980-07-02");
  27.  
  28. ?>
php