Dragosvr92 Posted September 30, 2012 Share Posted September 30, 2012 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. Quote Link to comment Share on other sites More sharing options...
kicken Posted September 30, 2012 Share Posted September 30, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 30, 2012 Share Posted September 30, 2012 (edited) 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 September 30, 2012 by Jessica Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 30, 2012 Share Posted September 30, 2012 You would subtract 1 if the mmdd of the birthday is less than the mmdd of the current date. Quote Link to comment Share on other sites More sharing options...
Solution salathe Posted September 30, 2012 Solution Share Posted September 30, 2012 $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. Quote Link to comment Share on other sites More sharing options...
Dragosvr92 Posted September 30, 2012 Author Share Posted September 30, 2012 Thank You salathe ! I managed to make my script display the full year (Y, not y), and the script works better. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 30, 2012 Share Posted September 30, 2012 You would subtract 1 if the mmdd of the birthday is less than the mmdd of the current date. or vice versa! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.