Home  >  Article  >  Backend Development  >  An age calculation function accurate to days written in PHP

An age calculation function accurate to days written in PHP

WBOY
WBOYOriginal
2016-07-25 09:05:51885browse
  1. /**
  2. * PHP age calculation function
  3. *
  4. * Parameters support array parameter passing and standard Mysql date type parameter passing
  5. * params sample
  6. * ------------------
  7. $birthArr = array(
  8. 'year' => '2000',
  9. 'month' => '11',
  10. 'day' => '3'
  11. );
  12. $birthStr = '2000-11-03';
  13. * ------------------
  14. * );
  15. * @author
  16. * @copyright (c) 2011,2012 Just Use It!
  17. * @link IT tumbler http:/ /yungbo.com http://bbs.it-home.org
  18. * @param string|array $birthday
  19. * @return number $age
  20. */
  21. function getAge($birthday) {
  22. $age = 0;
  23. $year = $month = $day = 0;
  24. if (is_array($birthday)) {
  25. extract($birthday);
  26. } else {
  27. if (strpos($birthday, '-') !== false) {
  28. list($year, $month, $day) = explode('-', $birthday);
  29. $day = substr($day, 0, 2); //get the first two chars in case of '2000-11-03 12:12:00'
  30. }
  31. }
  32. $age = date('Y') - $year;
  33. if (date('m') < $month || (date('m') == $month && date('d') < $day)) $age--;
  34. return $age;
  35. }
  36. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn