Heim  >  Artikel  >  Backend-Entwicklung  >  php写的一个精确到天的年龄计算函数

php写的一个精确到天的年龄计算函数

WBOY
WBOYOriginal
2016-07-25 09:05:51885Durchsuche
  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. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn