Problem calculating age

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Problem calculating age

Post by mikes1471 »

Hi Guys I have the following script in my profile page to calculate the age from yob mob and dob (year, month, day) from the DB

Code: Select all

//display username
echo "<br>";
$username = mysql_query("SELECT username FROM users WHERE id='$id'");
$firstname = mysql_query("SELECT firstname FROM users WHERE id='$id'");
$gender = mysql_query("SELECT gender FROM users WHERE id='$id'");
$city = mysql_query("SELECT city FROM users WHERE id='$id'");
$birthdate = mysql_query("SELECT birthdate FROM users WHERE id='$id'");
$yob = mysql_query("SELECT yob FROM users WHERE id='$id'");
$mob = mysql_query("SELECT mob FROM users WHERE id='$id'");
$dob = mysql_query("SELECT dob FROM users WHERE id='$id'");
$usernamearray = mysql_fetch_assoc($username);
$firstnamearray = mysql_fetch_assoc($firstname);
$genderarray = mysql_fetch_assoc($gender);
$cityarray = mysql_fetch_assoc($city);
$yobarray = mysql_fetch_assoc($yob);
$mobarray = mysql_fetch_assoc($mob);
$dobarray = mysql_fetch_assoc($dob);
 
 
echo $yob;
echo $mob;
echo $dob;
# Function to calculate someone's age
function getage($year,$month,$day) {
        # Get current date
        $cyear = date('Y');
        $cmon = date('m');
        $cday = date('d');
 
        # Change day/month to xx format if just x
        if(strlen($month) < 2) $month = '0'.$month;
        if(strlen($day) < 2) $day = '0'.$day;
        # If year is not in xxxx format assume 19xx
        if(strlen($year) < 4) $year = '19'.$year;
 
        # Get their age (roughly)
        $age = $cyear - $year;
 
        # Account for day/month
        if($cmon <= $month && $cday <= $day) {
                $age--;
        }
 
        return $age;
};
 
$theirage = getage($yob,$mob,$dob);
echo $theirage;
 
However, the echo from this is: Resource id #11Resource id #12Resource id #131998

when the values in the DB are yob '1979' mob '03' and dob is '14'

Any help or suggestions would be much appreciated, I'm lost!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem calculating age

Post by jackpf »

You're just echoing out the query, which is just a PHP resource handle.

You'll need to use *array['the_column'] where * is whatever you're doing (yob, mob and dob by the looks of things) and 'the_column' is whatever column you wish to retrieve.


EDIT
I've just actually read your code properly, and there's no need for three queries. You can select all rows with a wildcard (*) with just one query.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem calculating age

Post by jackpf »

mikes1471 wrote:Subject: Problem calculating age
jackpf wrote:You're just echoing out the query, which is just a PHP resource handle.

You'll need to use *array['the_column'] where * is whatever you're doing (yob, mob and dob by the looks of things) and 'the_column' is whatever column you wish to retrieve.


EDIT
I've just actually read your code properly, and there's no need for three queries. You can select all rows with a wildcard (*) with just one query.
Sorry to be a pain but I cant see where I need to make an ammendment in my code, the age calculator is producing the year 1998 at the moment rather than the age 30, I did think if I echo out the results of yob, mob and dob I could see where the problem was but I still cant understand how to echo the year month and day, what part of my code needs to be replaced and with what, sorry to be dense
What I mean is, you can just do this instead, which is a lot more efficient:

Code: Select all

$sql = mysql_query("SELECT * FROM users WHERE id='$id'");
$fetch = mysql_fetch_array($sql);
 
$username = $fetch['username'];
$firstname = $fetch['firstname'];
 
//...and so on....
And I think I made a suggestion on your other thread to store the DoB in a unix timestamp format. I don't really know what's going on with the rest of your code there.
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Re: Problem calculating age

Post by mikes1471 »

I had concatinated the birthdate in my DB at first which was similar to the unix timestamp other then the yyyy/mm/dd format rather than the timestamp yyyy-mm-dd format but as a friend supplied me with the following code:

Code: Select all

<?php
$yob = $_POST['yob'];
$mob = $_POST['mob'];
$dob = $_POST['dob'];
$cyear = date('Y');
$cmon = date('m');
$cday = date('d');
$age = $cyear - $yob;
if($cmon <= $mob && $cday <= $dob) {
        $age--;
}
echo $age;
?>
which required me to separate the year/month/day fields into yob, mob and dob

I appreciate the input so far but should probably mention I dont understand anyones suggestions, I've successfully echoed other values from my database and can successfully echo '1998' where my age should appear and I know its because my values are incorrect, they should read 'year' 'month' 'day' but instead they read 'Resource id #11Resource id #12Resource id #131998'

I'm on day 4 of trying to resolve this and I think its clear to anyone I'm nowhere nearer resolving it or I wouldnt be here lol so why am I getting the results 'Resource id #11Resource id #12Resource id #131998' and how do I replace these with the values Im looking for?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem calculating age

Post by jackpf »

With the code I posted you can use $fetch to retrieve any column from the database. And a unix timestamp is a number, I don't know what you mean by it's yyyy/mm/dd format, it's just a number.

Maybe you should look up a mysql/php tutorial.

This would honestly be a lot easier if you followed mine or others' suggestions in your other thread...
Post Reply