Another way to calculate age, given a MySQL date format


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function calculateAge ($birthdate)
  2. {
  3. $bdt = strtotime($birthdate);
  4.  
  5. if (false === $bdt || -1 === $bdt) {
  6. return 0;
  7. }
  8.  
  9. $birthDateParts = getdate($bdt);
  10. $todayDateParts = getdate();
  11.  
  12. $nYear = $todayDateParts['year'] - $birthDateParts['year'];
  13. $nMonth = $todayDateParts['mon'] - $birthDateParts['mon'];
  14. if ($nMonth < 1) {
  15. if ($nMonth == 0) {
  16. $nDay = $todayDateParts['mday'] - $birthDateParts['mday'];
  17. return ($nDay < 0) ? ($nYear - 1) : $nYear;
  18. }
  19.  
  20. return ($nYear - 1);
  21. }
  22.  
  23. return $nYear;
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.