• 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

    • Qualcomm takes on NVIDIA with new Dragonfly CPU and AI chips by Pradeep Viswanathan Microsoft, Google, Amazon, AMD, Meta, Apple, OpenAI, and several others have been developing their own chips for AI infrastructure. However, NVIDIA still remains the dominant player in the market. Today, Qualcomm announced a major expansion of its data center infrastructure portfolio to better compete with NVIDIA. The new lineup includes the Qualcomm Dragonfly C1000 CPU, Qualcomm High Bandwidth Compute technology, the Dragonfly AI300 inference accelerator, new connectivity products, and custom silicon solutions. Qualcomm claims that this new lineup improves performance per watt, token throughput, and total cost of ownership for AI data centers. The Dragonfly C1000 is a new data center CPU built with Qualcomm’s custom Oryon cores. This chip will feature more than 250 cores, frequencies above 5GHz, and a chiplet-based design. Qualcomm claims that this new C1000 can deliver more than 2x better performance per watt compared to existing server CPU offerings based on specifications. The Dragonfly C1000 will support PCIe Gen 7 with more than 2TB/s of connectivity, along with CXL, advanced RAS features, and both air and liquid cooling. Qualcomm expects the Dragonfly C1000 to be commercially available in 2028. Additionally, Qualcomm and Meta announced a multi-year, multi-generation agreement under which Qualcomm will supply Dragonfly C1000 data center CPUs for Meta’s next-generation server fleet. Qualcomm also announced High Bandwidth Compute, a new near-memory computing architecture designed to address AI’s memory bandwidth bottleneck. HBC Gen 1 will debut with the Dragonfly AI250, which is expected to sample in mid-2027. The AI250 will deliver 133TB/s per card, an 18x increase in effective memory bandwidth compared to the AI200 with LPDDR5X. The new Dragonfly AI300 with HBC Gen 2 is a rack-level AI inference platform from Qualcomm. Qualcomm claims that the AI300 can deliver 4x to 8x better performance per watt compared to existing GPU-based architectures based on memory bandwidth per watt per card. The Dragonfly AI300 is expected to be available in 2028.
    • IBM reveals sub-1nm chip technology, production expected in another 5 years by Pradeep Viswanathan TSMC is now leading the chip manufacturing industry with its 2nm-class process node called N2. Samsung Foundry also has a 2nm-class process node called SF2. TSMC says N2 entered volume production in Q4 2025. Samsung says SF2 started mass production in 2025. Today, IBM announced the world’s first sub-1-nanometer chip technology, marking another major semiconductor research milestone. The new technology is based on a 0.7nm, or 7-angstrom, node and uses a new transistor architecture called “nanostack.” The new design vertically stacks and staggers nanosheet-based transistors so that more components can fit into the same chip area while also improving performance and power efficiency. IBM claims that this new sub-1nm chip can pack nearly 100 billion transistors onto a chip the size of a fingernail. This offers almost twice the density, up to 50 percent higher performance, or 70 percent better energy efficiency when compared to IBM's 2nm node design announced back in 2021. Also, IBM mentioned that this new architecture can deliver 40 percent SRAM scaling. It is important to consider that this announcement from IBM is a research milestone rather than a near-term process node launch. Back in 2021, IBM unveiled the world’s first 2nm chip design, claiming 50 billion transistors on a fingernail-sized chip and major performance and efficiency gains. Five years later, IBM’s 2nm technology has still not entered mainstream commercial production. That is because IBM is no longer a major commercial chip manufacturer. It sold its chip manufacturing business to GlobalFoundries years ago and has since then focused only on semiconductor research, IP development, and partnerships. To productize its 2-nm chip technology, IBM partnered with Japan’s Rapidus, but it has not resulted in anything shipping at scale. IBM says that its new sub-1nm technology can reach production as early as within the next five years. If that happens, it will likely depend on manufacturing partners, advanced EUV tooling, and years of yield improvements.
  • Recent Achievements

    • Week One Done
      Meta Plast earned a badge
      Week One Done
    • First Post
      kinowa earned a badge
      First Post
    • Rookie
      krychek57 went up a rank
      Rookie
    • Grand Master
      Jaybonaut went up a rank
      Grand Master
    • One Year In
      Philsl earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      461
    2. 2
      +Edouard
      171
    3. 3
      PsYcHoKiLLa
      136
    4. 4
      Michael Scrip
      78
    5. 5
      Xenon
      77
  • Tell a friend

    Love Neowin? Tell a friend!