Posted By

phifty on 09/27/10


Tagged

time date AGE


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bobbym245


Another way to calculate age, given a MySQL date format


 / Published in: PHP
 

  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  

You need to login to post a comment.