acp26b Posted January 25, 2007 Share Posted January 25, 2007 Hi, I am sure this is pretty easy i just can not figure it out. I have a from where i submit an age range and it pulls all the clients out of the database within that age range that is accomplished with this:[code] $year = date(Y); $year1 = $year - $form_age1 ; $year2 = $year - $form_age2 ; $monthday = date("m-d"); $date1 = $year1 . "-" . $monthday; $date2 = $year2 . "-" . $monthday; $searchquery = "SELECT c.clientid, c.lname, c.fname, c.dateofbirth FROM clients c WHERE DATE(`DateOfBirth`) BETWEEN '$date2' and '$date1' and c.active = 'y' ";[/code]so then i have all the clients within a cretin age range and i have their birthday that i store in a variable called $db_dateofbirth, when i display the the list of results i want to display their age not their date of birth, how would i go about deriving the age? when i have todays date [ $today = date("yyyy-mm-dd") ] and their date of birth in the same format? Quote Link to comment Share on other sites More sharing options...
zq29 Posted January 25, 2007 Share Posted January 25, 2007 Something like this?[code]<?phpfunction age($dob) { return round((strtotime(date("Y/m/d"))-strtotime($dob))/(365*24*60*60),0);}echo age("1985/12/16");?>[/code] Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 25, 2007 Share Posted January 25, 2007 you might want to convert the two time vars into unix timestamps using mktime() function, then you can subtract the DOB from the current date, then use strftime() function to format the output.$unix_dob = mktime(0,0,0,$month,$day,$year);$today = time();$diff = $today - $unix_dob;$age = strftime('%y-%j', $diff);would output something like 10 years - 122 days Quote Link to comment Share on other sites More sharing options...
obsidian Posted January 25, 2007 Share Posted January 25, 2007 Or, I like this one (more along the lines of the first suggestion):[code]<?php$bday = "2/15/1980";$diff = abs(time() - strtotime($bday));$age = floor($diff / (60 * 60 * 24 * 365));?>[/code]Keep in mind the usage of floor() versus round(). With floor(), you guarantee you will show the actual age until each year is ended. Now, this is also a very generic way to count since it doesn't take into account any leap years at all. There are some more eloquent ways to handle [b]exact[/b] age matching, but typically, this is good enough for validation purposes since it only gets off one day every four years. Quote Link to comment Share on other sites More sharing options...
acp26b Posted January 25, 2007 Author Share Posted January 25, 2007 Thanks for all the fast replies!SemiApocalyptic, the funtion seems to top out at 37 all the clients that are over 37 return an age of 37, kind of weird and i can not figure out why.I am in the process of trying dgiberson's suggestion right now (working on tokenizing my date down to $year, $month, $day), when i tried the floor method explained by obsidian it retuned 0 for all ages, i think it has to to with a format issue. Thanks again for the replies, this gives me some stuff to work with Quote Link to comment Share on other sites More sharing options...
obsidian Posted January 25, 2007 Share Posted January 25, 2007 The max age is always going to be 37 [b]this year[/b] since the UNIX timestamp starts with 12am on January 1, 1970. That gets into the more specific targeting I mentioned above. I'm not sure why mine above is giving '0', though, but it's not worth fixing since it's going to have the same limitation as SA's :P Quote Link to comment Share on other sites More sharing options...
acp26b Posted January 25, 2007 Author Share Posted January 25, 2007 dang cause the real world use will be to find clients that are over 70. Oh well i guess i can just display the birthday and not age...thanks again for all the help Quote Link to comment Share on other sites More sharing options...
craygo Posted January 25, 2007 Share Posted January 25, 2007 you can use another class to get ages over 37. it is called adodb_date. It will let you calculate any ages. Download the file then include it and then you can use this[code]<?phpinclude("adodb-time.inc.php");function age($day, $month, $year){$today = date("Y");$born = adodb_date("Y", adodb_strftime(adodb_mktime(0,0,0,$month,$day,$year)));$age = $today-$born;return $age;}echo age("24", "12", "1945"); // will return 62?>[/code]Will have to do a little more code to get it to do a more exact ageRay[attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 25, 2007 Share Posted January 25, 2007 Maybe this will help:[code]<?phpfunction birthday($birthday){ list($month,$day,$year) = explode("-",$birthday); $year_diff = date("Y") - $year; $month_diff = date("m") - $month; $day_diff = date("d") - $day; if ($day_diff > 0 || $month_diff < 0){ $year_diff--; } return $year_diff; } echo birthday('07-22-1986'); // Returns 20 mm-dd-yyyy?>[/code] Quote Link to comment Share on other sites More sharing options...
acp26b Posted January 25, 2007 Author Share Posted January 25, 2007 Thanks little guy, worked perfect! One more newbie question for today, where is the solved button? I read the thread on top of board and it said to mark them solved when they are, but i think i am either dumb or blind cause i cant find it, neither can firefox (Ctrl +f) Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 25, 2007 Share Posted January 25, 2007 It went bye bye in an update. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 25, 2007 Share Posted January 25, 2007 The "topic solved" button should be located on the bottom, after the replies, just above the quick reply area.Ken 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.