MrLarkins.com Posted October 13, 2006 Share Posted October 13, 2006 ok, i $bday_day //birthday day$bday_month //birthday month$bday_year //birthday year$today = getdate();previously defined from a mysql grab i've done (the birthday stuff is in the database in columns bday_day, bday_month, bday_year)what i'd like to do is use this data to find out how old the person is and display it Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/ Share on other sites More sharing options...
gmwebs Posted October 13, 2006 Share Posted October 13, 2006 A quick Google search found me this... Should do the trick?[url=http://www.nbrandt.com/get-age-from-date.php]http://www.nbrandt.com/get-age-from-date.php[/url]HTH Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108519 Share on other sites More sharing options...
MrLarkins.com Posted October 13, 2006 Author Share Posted October 13, 2006 yea, i saw that, but it finds the age if you input a specific date, not exactly what i'm need/wanting.but i would use a function right? i know diddly-squat about those. Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108528 Share on other sites More sharing options...
redbullmarky Posted October 13, 2006 Share Posted October 13, 2006 [quote author=MrLarkins.com link=topic=111422.msg451578#msg451578 date=1160764904]but i would use a function right? i know diddly-squat about those.[/quote]it's impossible to use PHP without some knowledge of functions. echo itself is a function. mysql_query is a function. the list goes on. i've written a function i often use for this purpose:[code]<?php// returns a persons age given their date of birthfunction getage($dob){ $exp = explode('-', $dob); // we now have the year[0], month[1] and date[2] $current = getdate(); // get an estimate of years $ret = $current['year'] - $exp[0]; if ($ret > 110) { return 0; // too old - possible no date parameter was specified } // have we reached their birthday month? if ($current['mon'] <= $exp[1]) { $ded = 1; // we arent gonna DEDuct a year yet // are we in their birthday month? if (($current['mon'] == $exp[1]) && ($current['mday'] >= $exp[2])) { $ded = 0; } $ret -= $ded; // deduct a year as this years birthmonth not arrived } return $ret;}?>[/code]all it needs is a date of birth (YYYY-MM-DD format) and it'll return the age. so:[code]$age = getage('1980-01-16');echo $age; // 26[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108530 Share on other sites More sharing options...
Barand Posted October 13, 2006 Share Posted October 13, 2006 A person born in 1986 is 20 this year, BUT, if the birthday is later in the year than today, they are still only 19, so, putting this logic into a function[code]<?phpfunction getAge ($bday, $bmon, $byr) { $ynow = date('Y'); $age = $ynow - $byr; // format birth month and day as "0901" $bmd = sprintf ('%02d%02d', $bmon, $bday); // format today monthand day in same way $mdnow = date('md'); if ($bmd > $mdnow) $age -= 1; // sub 1 from age if not there yet return $age;}// call the function to get the age$age = getAge ($bday_day, $bday_month, $bday_year);echo $age;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108536 Share on other sites More sharing options...
MrLarkins.com Posted October 13, 2006 Author Share Posted October 13, 2006 hey, that looks like a winner. but it leaves me with two new problems1] my birthday data is from seperate columns in the database. how would i merge them to make it the necessary formatting for your code?2] my birthday data is of the form YYYY for year, M for month, and D for day, when it needs to be (YYYY-MM-DD). i.e. the 1st shows as '1' not '01' and August shows as '8' not '08'. would i use a push to add the preceeding zeros? Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108541 Share on other sites More sharing options...
Barand Posted October 13, 2006 Share Posted October 13, 2006 ??? ???1 ) my function passes the date as the 3 separate fields2 ) it reformats month "1" as "01" and day as "01" Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108551 Share on other sites More sharing options...
MrLarkins.com Posted October 13, 2006 Author Share Posted October 13, 2006 lol, Barand my response was to the other guy, you posted while i was typing,lol Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108555 Share on other sites More sharing options...
redbullmarky Posted October 13, 2006 Share Posted October 13, 2006 and to be fair, the function I use is one I wrote ages back and whilst it's worked for me, it's a bit crusty. looking at the one provided by Barand, his is much simpler - I'll probably be switching very soon :) Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108568 Share on other sites More sharing options...
MrLarkins.com Posted October 13, 2006 Author Share Posted October 13, 2006 hmm, barand, should i modify the function getAge ([b]$bday, $bmon, $byr[/b]) to show bday_day,bday_month,bday_year? i copied and pasted it straight into place and it gave me an error on that line Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108570 Share on other sites More sharing options...
Barand Posted October 13, 2006 Share Posted October 13, 2006 The variable names in the function definition are irrelevant, they could be $a, $b, $c.It depends on what your code is to call the function.@RedBull,A problem with passing YYYY-MM-DD then using php datetime functions to convert is that it doesn't work if the DOB is earlier than 1970 with older version of PHP. Later ones create -ve timestamp values for older dates.PS a general observation, your explodes the "YYYY-MM-DD" date. Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108578 Share on other sites More sharing options...
MrLarkins.com Posted October 13, 2006 Author Share Posted October 13, 2006 ok, so why do i get the error?[url=http://www.ice-squad.com/Memberslist.php]http://www.ice-squad.com/Memberslist.php[/url] <--see here Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108581 Share on other sites More sharing options...
Barand Posted October 13, 2006 Share Posted October 13, 2006 That error is telling you that the function is defined more than once in your code. Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108585 Share on other sites More sharing options...
MrLarkins.com Posted October 13, 2006 Author Share Posted October 13, 2006 hmm, i don't see where it is...you look...[code]<?php//----------------------------// Require//----------------------------require "forums/conf_global.php"; //because this file will not be located in the forums directory.//-------------------------------// Connect To MySQL//-------------------------------mysql_connect($INFO['sql_host'], $INFO['sql_user'], $INFO['sql_pass']) or die(mysql_error());mysql_select_db($INFO['sql_database']) or die(mysql_error());$define_country = array("u"=>"United States","e"=>"England","c"=>"Canada","m"=>"Mexico","a"=>"Aruba","o"=>"Other");$define_state = array("AL"=>"Alabama","AK"=>"Alaska","AZ"=>"Arizona","AR"=>"Arkansas","CA"=>"California","CO"=>"Colorado","CT"=>"Connecticut","DE"=>"Delaware","DC"=>"District of Columbia","FL"=>"Florida","GA"=>"Georgia","HI"=>"Hawaii","ID"=>"Idaho","IL"=>"Illinois","IN"=>"Indiana","IA"=>"Iowa","KS"=>"Kansas","KY"=>"Kentucky","LA"=>"Louisiana","ME"=>"Maine","MH"=>"Marshall Islands","MD"=>"Maryland","MA"=>"Massachusetts","MI"=>"Michigan","MN"=>"Minnesota","MS"=>"Mississippi","MO"=>"Missouri","MT"=>"Montana","NE"=>"Nebraska","NV"=>"Nevada","NH"=>"New Hampshire","NJ"=>"New Jersey","NM"=>"New Mexico","NY"=>"New York","NC"=>"North Carolina","ND"=>"North Dakota","MP"=>"Northern Mariana Islands","OH"=>"Ohio","OK"=>"Oklahoma","OR"=>"Oregon","PA"=>"Pennsylvania","PR"=>"Puerto Rico","RI"=>"Rhode Island","SC"=>"South Carolina","SD"=>"South Dakota","TN"=>"Tennessee","TX"=>"Texas","UT"=>"Utah","VT"=>"Vermont","VI"=>"Virgin Islands","VA"=>"Virginia","WA"=>"Washington","WV"=>"West Virginia","WI"=>"Wisconsin","WY"=>"Wyoming","AB"=>"Alberta","BC"=>"British Columbia","MB"=>"Manitoba","NB"=>"New Brunswick","NL"=>"Newfoundland and Labrador","NT"=>"Northwest Territories","NS"=>"Nova Scotia","NU"=>"Nunavut","ON"=>"Ontario","PE"=>"Prince Edward Island","QC"=>"Quebec","SK"=>"Saskatchewan","YT"=>"Yukon","OO"=>"Other");$define_character = array("m"=>"Medic","g"=>"Gunner","r"=>"Rifleman","s"=>"Sniper","e"=>"Engineer");//Start HTML Tableprint('<table cellspacing=0 cellpadding=0 width=100%>');//-----------------------------------//Get Info//-----------------------------------$main_result = mysql_query("SELECT m.id, m.name, m.email, m.joined, m.bday_day, m.bday_month, m.bday_year, x.icq_number, c.field_1, c.field_2, c.field_3, c.field_4FROM ibf_members AS mINNER JOIN ibf_member_extra AS x ON m.id = x.idINNER JOIN ibf_pfields_content AS c ON m.id = c.member_idWHERE mgroup=7ORDER BY id");while($main = mysql_fetch_array($main_result)) { //while you have records available from the SELECT query $id = $main['id']; $name = $main['name']; $email = $main['email']; $join_date = $main['joined']; $bday_day = $main['bday_day']; $bday_month = $main['bday_month']; $bday_year = $main['bday_year']; $icq = $main['icq_number']; $stats = $main['field_1']; $character = $main['field_2']; $country = $main['field_3']; $state = $main['field_4'];function getAge ($bday, $bmon, $byr) { $ynow = date('Y'); $age = $ynow - $byr; // format birth month and day as "0901" $bmd = sprintf ('%02d%02d', $bmon, $bday); // format today monthand day in same way $mdnow = date('md'); if ($bmd > $mdnow) $age -= 1; // sub 1 from age if not there yet return $age;}// call the function to get the age$age = getAge ($bday_day, $bday_month, $bday_year);echo $age;echo ("<tr><td>$name </td><td>JOINED $join_date</td><td><img src='images/flags/$define_country[$country].gif' border=1 alt='$define_country[$country]'> <img src='images/flags/$define_state[$state].gif' border=1 alt='$define_state[$state]'></td><td><a href='http://web.icq.com/whitepages/add_me?uin=$icq_number&action=add'><font size='-1'>$icq_number</font></a></td><td><a href='mailto:$email'><font size='-1'>Email</font></a></td><td>AGE $age</td><td><a href='$stats'><font size=-1><b><u>JO Stats</u></b></a></td><td>$define_character[$character]</td></tr>");}//Finish the HTML tableprint('</table>');?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108589 Share on other sites More sharing options...
redbullmarky Posted October 13, 2006 Share Posted October 13, 2006 you've wrapped your function in your 'while' loop - meaning it'll get redeclared on each iteration.move the function to the top of your script - underneath the [code]<?phprequire "forums/conf_global.php"; //because this file will not be located in the forums directory.?>[/code]will do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108591 Share on other sites More sharing options...
MrLarkins.com Posted October 13, 2006 Author Share Posted October 13, 2006 argh, now i have extra numbers! Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108593 Share on other sites More sharing options...
MrLarkins.com Posted October 13, 2006 Author Share Posted October 13, 2006 lol, i'm such a bonehead sometimes...its all good, thanks for the help, this topic is done... Quote Link to comment https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/#findComment-108598 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.