ホームページ  >  記事  >  バックエンド開発  >  PHPで書かれた日数まで正確な年齢計算機能

PHPで書かれた日数まで正確な年齢計算機能

WBOY
WBOYオリジナル
2016-07-25 09:05:51885ブラウズ
  1. /**
  2. * PHP 年齢計算関数
  3. *
  4. * パラメータは配列パラメータの受け渡しと標準の Mysql 日付型パラメータの受け渡しをサポート
  5. * params サンプル
  6. * ------------------
  7. $birthArr = array(
  8. '年' => '2000',
  9. '月' => '11',
  10. '日' => '3'
  11. ); * ------------------
  12. * );
  13. * @author
  14. * @copyright (c) 2011,2012 Just Use It!
  15. * @link IT タンブラー http:/ /yungbo.com http://bbs.it-home.org
  16. * @param string|array $birthday
  17. * @return number $age
  18. */
  19. function getAge($birthday) {
  20. $age = 0;
  21. $年 = $月 = $日 = 0;
  22. if (is_array($birthday)) {
  23. extract($birthday);
  24. } else {
  25. if (strpos($birthday, '-') !== false) {
  26. list($year, $month, $day) =explode('-', $birthday);
  27. $day = substr($day, 0, 2); //「2000-11-03 12:12:00」の場合、最初の 2 文字を取得します
  28. }
  29. }
  30. $age = date('Y') - $year;
  31. if (date('m') < $month || (date('m') == $month && date('d') < $day)) $age--;
  32. $age を返す;
  33. }
  34. ?>
复制代
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。