Jump to content

Calculate Age From Birth Year.


Go to solution Solved by salathe,

Recommended Posts

I have a script that outputs the birthyear in the dd.mm.yy format. Example 15.02.92.

Does anyone have a clue how may i calculate the age against the current, autogenerated date?

I just want the straight age. Not the Day/month. But i wouldnt mind if it does both. Thanks in advance.

 

PS: This is the second topic i create on this new forums. Does it still use SMF?

The "Textarea" which seems to be an iframe, and not a textarea is annoying me. I dont have the copy/paste menu when i right click in it. The undo option is also a bit screwy.

Link to comment
https://forums.phpfreaks.com/topic/268938-calculate-age-from-birth-year/
Share on other sites

If you just want the age by year then all you gotta do is subtract the birth year from the current year, eg:

list($dd, $mm, $yy) = explode('.', $birthday);
$age = date('Y') - ($yy + $yy>=50?1900:2000);

 

The $yy + $yy>=50?1900:2000 converts the two-digit year to a four-digit year by adding either 1900 or 2000 depending on a particular year break point. I chose 50 which means something like 19.7.45 would be 2045 while something like 19.7.65 would be 1965. You could adjust the break point to whatever you want.

 

If you want the more exact age down to the day, then you'd run through a process like so:

$age = $ynow - $year;
Are the years equal? 
 yes => 
   Is the current month less than the birth month?
     yes => $age = $age - 1;
     no => 
       Is the current month equal to the birth month?
         yes => 
           Is the day less than the birth day?
             yes => $age = $age - 1;

 

There are lots of implementations of an age function on the internet if you want to just google for one.

 

PS: This is the second topic i create on this new forums. Does it still use SMF?

No, it uses IP.Board now.

If you just want the age by year then all you gotta do is subtract the birth year from the current year, eg:

If the person hasn't had their birthday yet this year, you'll be off by a year...If you subtract 2012-1974 you'll get 38. Yet my husband is only 37.

 

I realize you addressed how to do the actual age below but I don't think the OP really wants just the difference in years, that's not an age.

Edited by Jessica
  • Solution

$subject = '15.02.92';
$interval = DateTime::createFromFormat('d.m.y', $subject)->diff(new DateTime);
echo $interval->y;

 

The above uses the DateTime (docs) class (two instances of it; one for the current date/time and one for the date of birth) and a DateInterval (docs) object representing the interval between those two DateTimes.

 

The DateInterval has several properties available including the number of years; which for our needs is the person's age.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.