• 0

[PHP/SQL] Calculate Age


Question

16 answers to this question

Recommended Posts

  • 0

Never done this but could be something along:

age = current_year - birth_year;

if(current_date < birth_date && current_month < birth_month){
    age = age - 1;
}

Assuming you store months with their numerical value which is the better way IMHO.

  • 0

The PHP website is reading straight off the table called members. I havent created any views or anything. Im also using phpMyAdmin to create all the database.

Where would I type in that statement?

SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age

  • 0

You would execute it where required; it's no different to any of the other queries you're executing.

SELECT
    id
  , DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') &lt; DATE_FORMAT(dob, '00-%m-%d')) AS age
  , name
FROM
  member

  • 0

You would execute it where required; it's no different to any of the other queries you're executing.

SELECT
    id
  , DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') &lt; DATE_FORMAT(dob, '00-%m-%d')) AS age
  , name
FROM
  member

There are no queries. which means im going somethign wrong here :s

Ok... Let me explain this situation a little better.

The database has information on members and their date of birth is stored. I have created a form using PHPRunner which creates the form from the members table. Since age cannot be stored into the database. The software has the ability to input functions or PHP code snippets.

So i was thinking, rather can creating queries, i would use php code instead to calculate the age. The php website can add and view members from the database.

hope this helps

  • 0

you can just use DATEDIFF, and then round the end figure down after dividing the number of days in a year

i.e. my birthday is 11 oct 1984

select datediff('11-oct-1984',now())/365

that query results in 26, which is correct.. i am not a MySQL or PHP Dev so please test this first, it works in MSSQL anyway :p

There are no queries. which means im going somethign wrong here :s

well, how are you going to get the data out of the db? magic? :blink:

you will need to query the db in order to get the DOB, so, why not in your query convert it to the age rather than get php to do it for you on the page?

if you want to get the DOB from the db, and then convert it on the page via PHP.. try this function: http://www.bradino.com/php/calculate-age/ ;)

  • 0

I see.

function getAge($birthday){
  list($day, $month, $year) = explode('/', $birthday);
  $age        = date('Y') - $year;
  $month_diff = date('m') - $month;
  $day_diff   = date('d') - $day;
  if($day_diff &lt; 0 || $month_diff &lt; 0){
    $age--;
  }
  return $age;
}

I was going to suggest DateTime:diff but it is 5.3 dependent.*

*date_diff is an alias for DateTime::diff

  • 0

Thanks guys, got it working. I used the following query to calculate the code

SELECT *, DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(aDoB, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') &lt; DATE_FORMAT(aDoB, '00-%m-%d')) AS age FROM members

And then I saved it as a View. I then created the PHP table using the view and now the age automatically update.

TYVM

  • 0

Thanks guys, got it working. I used the following query to calculate the code

SELECT *, DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(aDoB, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') &lt; DATE_FORMAT(aDoB, '00-%m-%d')) AS age FROM members

And then I saved it as a View. I then created the PHP table using the view and now the age automatically update.

TYVM

looks a bit inefficient, not sure though, would have to test it out

glad it works :)

  • 0

you can just use DATEDIFF, and then round the end figure down after dividing the number of days in a year

i.e. my birthday is 11 oct 1984

select datediff('11-oct-1984',now())/365

that query results in 26, which is correct.. i am not a MySQL or PHP Dev so please test this first, it works in MSSQL anyway :p

Leap years will throw that off so it is inaccurate.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Microsoft kills AI-powered history search feature in Edge by Usama Jawad In June 2025, Microsoft began rolling out AI-powered history search functionality with Edge 138. The idea was simple: allow customers to use natural language phrases and synonyms to find their desired history items rather than matching keywords exactly. Although the company had already rolled out this capability in a phased manner, it has now decided to cancel it. In an update on its Microsoft 365 Roadmap, Microsoft has announced that it has decided not to move forward with AI-powered history search. The company has not detailed the exact reasoning behind this move, but it has apologized to customers for the inconvenience. The move is rather interesting as it seemingly could have improved user productivity. Edge users wouldn't have to worry about typos or exact keywords, and just focus on what they were trying to locate in their browser history. Microsoft had also assured users that an on-device AI model would be leveraged for this functionality, and no data would be sent to the cloud. IT admins also had the ability to control its availability through the EdgeHistoryAISearchEnabled policy. When the feature began rolling out last year, many of our readers called it creepy, noting that they couldn't trust Microsoft to keep their data on their device. Others also questioned its usefulness, saying that it's simply a way for Microsoft to insert more AI bloat into its products. Although the Redmond tech giant had stated that it will be more mindful about surfacing Copilot features in Windows 11 apps, we later discussed how this is mostly a rebranding exercise rather than an actual axing of AI functionalities. Indeed, a Microsoft executive later emphasized how they want to reshape Windows for the agentic AI era. That said, it does seem like at least AI-powered Edge history search isn't a part of that vision.
    • For you to jump on and defend your sweetheart? I agree, it didn't.
    • Not likely, of course, that we will see routine citizenship checks as they only involve compliance with existing federal voting laws already on the books for decades. This seems a non-issue entirely. Only fools would have the government rush headlong into an AI program few can understand or predict, or even debug...
  • Recent Achievements

    • Week One Done
      tuben earned a badge
      Week One Done
    • First Post
      OffsetAbs earned a badge
      First Post
    • Reacting Well
      OffsetAbs earned a badge
      Reacting Well
    • First Post
      Kolakid60 earned a badge
      First Post
    • Week One Done
      xvvxcvv earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      424
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      147
    4. 4
      Steven P.
      72
    5. 5
      FloatingFatMan
      71
  • Tell a friend

    Love Neowin? Tell a friend!