DeanWhitehouse Posted July 3, 2008 Share Posted July 3, 2008 I dunno where to start with this, i need to work out the age of a person from there date of birth. the date is wrote like. daymonthyear 28081991 any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/113098-solved-work-out-age-from-date-of-birth/ Share on other sites More sharing options...
DarkWater Posted July 3, 2008 Share Posted July 3, 2008 You just need how many years old they are right, not like...days? Quote Link to comment https://forums.phpfreaks.com/topic/113098-solved-work-out-age-from-date-of-birth/#findComment-581009 Share on other sites More sharing options...
DeanWhitehouse Posted July 3, 2008 Author Share Posted July 3, 2008 erm, yer how many years, but i also need to work out that they are say. e.g. bday = 28/08/91 then they will be 16 now not 17 but after then they will be 17 . If that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/113098-solved-work-out-age-from-date-of-birth/#findComment-581038 Share on other sites More sharing options...
rajivgonsalves Posted July 3, 2008 Share Posted July 3, 2008 You can try <?php $strDate = "28081991"; echo floor((time()-strtotime(substr($strDate,4,4)."-".substr($strDate,2,2)."-".substr($strDate,0,2)))/(86400*365)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/113098-solved-work-out-age-from-date-of-birth/#findComment-581056 Share on other sites More sharing options...
Barand Posted July 3, 2008 Share Posted July 3, 2008 see http://www.phpfreaks.com/forums/index.php/topic,180194.msg803782.html#msg803782 Quote Link to comment https://forums.phpfreaks.com/topic/113098-solved-work-out-age-from-date-of-birth/#findComment-581089 Share on other sites More sharing options...
DeanWhitehouse Posted July 6, 2008 Author Share Posted July 6, 2008 @rajivgonsalves I used your code $day = trim(htmlentities($_POST['age'])); $month =trim(htmlentities($_POST['age1'])); $year =trim(htmlentities($_POST['age2'])); $dob = $day.$month.$year; //Work out birthday $strDate = $dob; echo floor((time()-strtotime(substr($strDate,4,4)."-".substr($strDate,2,2)."-".substr($strDate,0,2)))/(86400*365)); //end bday but this works for my birthday and probably more, but when i submitted a form with the birthday being 01011920 it got the age 38. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/113098-solved-work-out-age-from-date-of-birth/#findComment-582898 Share on other sites More sharing options...
DeanWhitehouse Posted July 6, 2008 Author Share Posted July 6, 2008 Ok, i solved it, i need to have it so it's like 01011920 instead of 1011920 Quote Link to comment https://forums.phpfreaks.com/topic/113098-solved-work-out-age-from-date-of-birth/#findComment-582909 Share on other sites More sharing options...
corbin Posted July 6, 2008 Share Posted July 6, 2008 You just need years, right? Different solution of the one earlier presented. In my opinion, this one is cleaner. Not sure if it's more efficient. <?php $month = 2; $day = 18; $year = 1992; $tyear = (int) date('Y'); $tmonth = (int) date('n'); $tday = (int) date('j'); $years = $tyear - $year - 1; if($tmonth > $month || ($tmonth == $month && $tday >= $day)) { //oddly written, but i think that's the most effecient way to do it. ++$years; } //if the current date is past (or equal to) the birthday, add a year echo $years; ?> Edit: Bleh, just realized the "if($month <= date('n') && $day <= date('j')) ++$years;" part is wrong. Since you already have a solution, I'm not gonna bother fixing it, unless you're interested in this. Edit: Fixed it, because it bothered me not having it correct. lol Quote Link to comment https://forums.phpfreaks.com/topic/113098-solved-work-out-age-from-date-of-birth/#findComment-583020 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.