2

This is the query I am using and it is in page 1 not inside the function.

<?php
$sql2= mysql_query("SELECT *
FROM catego WHERE category_id = '$idc'");
$categoryCount = mysql_num_rows($sql2); 
if ($categoryCount>0 )
{
    $row2 = mysql_fetch_array($sql2);     
    $id = $row2["category_id"];


    $birthdate = $row2["birthdate"];
    $address = $row2["address"];
}
?>
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58
fello
  • 339
  • 3
  • 10
  • What language are you programming in? EDIT - PHP I see – Pete Hamilton May 09 '11 at 14:30
  • 2
    Why is it formatted in that bizarre way? – Lightness Races in Orbit May 09 '11 at 14:33
  • 1
    possible duplicate of [How do I easily determine the age from an birthday? (php)](http://stackoverflow.com/questions/1203651/how-do-i-easily-determine-the-age-from-an-birthday-php) – deceze May 09 '11 at 14:33
  • possible duplicate of [PHP calculate age](http://stackoverflow.com/questions/3776682/php-calculate-age) – Jürgen Thelen May 09 '11 at 14:36
  • possible duplicate of http://stackoverflow.com/questions/3845208/php-calculating-birthday-from-age – Pete Hamilton May 09 '11 at 14:41
  • possible duplicate of http://stackoverflow.com/questions/2533890/get-an-age-from-a-d-o-b-field – Pete Hamilton May 09 '11 at 14:41
  • Does this answer your question? [How to get an age from a D.O.B field in MySQL?](https://stackoverflow.com/questions/2533890/how-to-get-an-age-from-a-d-o-b-field-in-mysql) – almaruf Jun 10 '22 at 10:28
  • Get age from DoB in your SQL and then just use it in PHP. Follow this https://stackoverflow.com/questions/2533890/how-to-get-an-age-from-a-d-o-b-field-in-mysql/22504155#22504155 – almaruf Jun 10 '22 at 10:29
  • Use SQL like this SELECT category_id, address, TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) AS age FROM catego WHERE category_id = NNN – almaruf Jun 10 '22 at 10:32

4 Answers4

4
SELECT YEAR(SUBDATE(NOW(), INTERVAL DATE_FORMAT("1975,09,02", "%Y-%m") YEAR_MONTH)) fs_age;
akond
  • 15,865
  • 4
  • 35
  • 55
  • Isn't this SQL? They are using PHP – Pete Hamilton May 09 '11 at 14:36
  • It is. He used the word "field", so I figured out that SQL is more appropriate. Besides, have a look at the subject. – akond May 09 '11 at 14:38
  • This select is not picking any fields from the database? my field is birthdate – fello May 09 '11 at 14:49
  • So replace the date in the example with your field. ... DATE_FORMAT(birthdate, "%Y-%m") ... – Colin May 09 '11 at 14:52
  • Coling do I have to do a function for this one or just iterate it once – fello May 09 '11 at 14:55
  • Use that SQL when querying the database - ie. SELECT first_name, last_name, other_field, YEAR(SUBDATE(NOW(), INTERVAL DATE_FORMAT(birthdate, "%Y-%m") YEAR_MONTH)) age FROM your_mysql_table; – Colin May 09 '11 at 14:57
  • But I don't know where to structure it Will I have a query in the same page where birthdate field is selected, I would like to use that query instead of westing it for this case. I was wondering if I can use a function instead and call it. – fello May 09 '11 at 15:17
  • coling the query I have is above can you take a look – fello May 09 '11 at 15:49
1

A simple php function could be something like the following:

function birth_date ($birth_date){
    list($y,$m,$d) = explode(",",$birth_date);
    $y_diff  = date("Y") - $y;
    $m_diff = date("m") - $m;
    $d_diff   = date("d") - $d;
    if ($m_diff < 0 || $d_diff < 0) { $y_diff--; }
    return $y_diff;
}

i.e split on your commas, work out the differences, adjust for which side of their birthdate the day/month is, if so reduce the year by one, then just return the year.

Then in your page you can put:

<li><span class="label">Age:</spa><?php$age = getage($birthdate); echo $age;?> </li>

EDIT:

<?php
function birth_date($birth_date){
    list($y,$m,$d) = explode(",",$birth_date);
    $y_diff  = date("Y") - $y;
    $m_diff = date("m") - $m;
    $d_diff   = date("d") - $d;
    if ($m_diff < 0 || $d_diff < 0) { $y_diff--; }
    return $y_diff;
}

$sql2= mysql_query("SELECT * FROM catego WHERE category_id = '$idc'");
$categoryCount = mysql_num_rows($sql2); 
if ($categoryCount>0 )
{
    $row2 = mysql_fetch_array($sql2);     
    $id = $row2["category_id"];
    $birthdate = $row2["birthdate"];
    $address = $row2["address"];

    $age = birth_date($birthdate);
}
?>
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58
  • Peter how will the function know, which memeber is it picking up? Do I have to do a SELECT somewhere to specify the code which memeber is that we are refering? – fello May 09 '11 at 14:51
  • How do I call this functino peter? I am calling it like and it throws an error Calling undefined function. – fello May 09 '11 at 15:15
  • The function as I wrote it is called birth_date. So you would put: $age = birth_date("2000,03,03"); echo $age; or your $row['birthdate'] equivalent. – Pete Hamilton May 09 '11 at 15:21
  • ok let me see what is wrong it seems like I did that but let me check just in case, above I have edit the original post you can see it is similar to what you just said. – fello May 09 '11 at 15:23
  • Also, in PHP unless in a class it should be "function myfunc(args){}" not "public function myfunc(args){}" – Pete Hamilton May 09 '11 at 15:25
  • ALSO: you haven't added PHP tags:
  • Age:
  • – Pete Hamilton May 09 '11 at 15:28
  • Or if using shorthand = getage($birthdate); ?> – Pete Hamilton May 09 '11 at 15:28
  • ok Yes I am not in a class! cool let me get the public function out – fello May 09 '11 at 15:28
  • I have updated my answer for you. I would be grateful if, after asking me for all this help on my solution, you actually marked it as the correct answer... – Pete Hamilton May 09 '11 at 15:30
  • Though, for the record, doing it in SQL is nicer. If you post your query I can update my answer to show how it should be done. – Pete Hamilton May 09 '11 at 15:31
  • hey I was off for a while I will try it now Sr. – fello May 09 '11 at 18:50
  • it works peter, the only thing is that I put the function in a function.php file and call it in another file. the functions is require_once in the file include_once"scripts/functions.php"; but it didn't pull up the birth_date function, inside the query. But once I put the function inside the query within the scope of the query then it worked. thought I wanted to keep it nice and clean all apart – fello May 09 '11 at 19:03