Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/23879-php-to-find-age-solved/
Share on other sites

[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 birth
function 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]
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]
<?php
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;

?>
[/code]

hey, that looks like a winner.  but it leaves me with two new problems


1]  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? 
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.
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 Table
print('<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_4
FROM ibf_members AS m
INNER JOIN ibf_member_extra AS x ON m.id = x.id
INNER JOIN ibf_pfields_content AS c ON m.id = c.member_id
WHERE mgroup=7
ORDER 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 table
print('</table>');

?>[/code]
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]
<?php
require "forums/conf_global.php"; //because this file will not be located in the forums directory.
?>
[/code]

will do the trick.
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.