>  기사  >  백엔드 개발  >  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') return $age;
  34. }
  35. ?>
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.