首頁  >  文章  >  後端開發  >  php寫的一個精確到天的年齡計算函數

php寫的一個精確到天的年齡計算函數

WBOY
WBOY原創
2016-07-25 09:05:51885瀏覽
  1. /**
  2. * PHP 年龄计算函数
  3. *
  4. * 参数支持数组传参和标准的 Mysql date 类型传参
  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不倒翁 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. ?>
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn