Posted By

Abe on 01/30/08


Tagged

get AGE calculate bithday


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

adix
tikitakfire
silentbob84


Calculate Age


 / Published in: PHP
 

URL: http://snippets.dzone.com/posts/show/1310

  1. <?php
  2. function birthday ($birthday){
  3. list($year,$month,$day) = explode("-",$birthday);
  4. $year_diff = date("Y") - $year;
  5. $month_diff = date("m") - $month;
  6. $day_diff = date("d") - $day;
  7. if ($month_diff < 0 || ($month_diff==0 && $day_diff < 0)) # Fix by tobiwan
  8. $year_diff--;
  9. return $year_diff;
  10. }
  11. echo "Age is: " . birthday ("1985-19-03");
  12. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: tobiwan on January 31, 2008

Hi! The if statement is a little bit too short...

if ($month_diff < 0 || ($month_diff==0 && $day_diff < 0))
[...]

should do the job. Tobias

You need to login to post a comment.