• 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

    • Bose Ultra Open Earbuds are once again selling at their lowest price by Fiza Ali Amazon is once again offering the Bose Ultra Open Earbuds at their lowest price ever with a limited-time 33 percent discount on their original MSRP, ahead of Father's Day. So, you may want to check it out if you are looking for a gift or if you have been wanting to upgrade your device. The earbuds feature an open-ear design and Bose's OpenAudio technology that should deliver high-quality sound while helping keep audio private. The earbuds also support Bose Immersive Audio, which creates a spatialised listening experience designed to place sound around the listener for a more engaging experience. In terms of wireless connectivity, the earbuds features Bluetooth, Bluetooth Low Energy (BLE), A2DP audio streaming, HFP, AAC, and SBC support. Furthermore, they are compatible with Bose SimpleSync technology, allowing pairing with compatible Bose smart soundbars and speakers. They are also compatible with the Bose App for setup, customisation, and software updates. Moreover, they offer an IPX4 water-resistance rating that should provide protection against sweat and light splashes. When it comes to the battery performance, the Bose Ultra Open Earbuds should provide up to seven hours of battery life on a single charge while a full recharge should take approximately one hour. Specifications Detail Fit type Open-ear Noise cancelling No Microphone Built-in Wireless Bluetooth (A2DP, HFP, AAC, SBC, BLE) Multipoint Yes; 2 devices simultaneously Charging interface USB-C Earbud size 0.73"x0.67" x 1.07" (0.014lb) Case size 1.65"x2.56" x 1.04" (0.097 lb) Materials PC-ABS plastic, metal, silicone, gold plating App support Bose app; adjustable EQ, SimpleSync Audio tech OpenAudio, immersive/spatialized sound Bose Ultra Open Earbuds: $199 (Amazon US) - 33% off Good to know This Amazon deal is U.S. specific, and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
    • After enabling it in about:config, customize, density, compact; the toolbar/address bar gets smaller vertically. I enabled Nova, I notice the tab bar/title bar is a bit larger vertically now? Everything always becomes a waste of space.
    • Microsoft's Copilot Cowork now generally available with usage-based billing by Pradeep Viswanathan Back in March, Microsoft first revealed Copilot Cowork, a new agentic AI experience in Microsoft 365 Copilot through which users can assign tasks to AI to complete in the background. After testing the service with a limited set of customers in Research Preview for a few weeks, Microsoft announced the general availability of Copilot Cowork to customers in the Frontier program on March 30. Today, Microsoft announced the general availability of Copilot Cowork worldwide for Microsoft 365 Copilot customers. The company also highlighted that Cowork became the fastest-growing feature in the history of its Frontier program. Unlike regular Copilot Chat, Copilot Cowork can run complex, long-running, multi-tool tasks from start to finish in the cloud by using organizational context through Work IQ. When compared to Claude Cowork, Microsoft claims that Copilot Cowork will be 30% to 40% cheaper on average with its Microsoft 365 connector. For now, Copilot Cowork runs on Anthropic models, including Opus 4.8 and Sonnet 4.6. However, Frontier customers can now use GPT-5.5. Microsoft also announced Cowork 1, a secure fine-tuned model coming in the next few weeks, which is designed to handle everyday Copilot tasks at a lower cost. To access Copilot Cowork, a Microsoft 365 Copilot user subscription is required. Usage is billed separately through Copilot Credits, based on model use, context retrieval, tool calls, and runtime. Pay-as-you-go pricing is set at $0.01 per Copilot Credit. To offer IT teams full control over usage costs, Microsoft provides spending limits, usage alerts, user-level controls, reporting, and prepaid usage plans for organizations. Usage-based billing begins today. However, Frontier customers who used Cowork between March 30 and June 16 will not be billed until July 1, 2026. The Microsoft 365 Copilot app now includes a toggle to enter the full Cowork experience. Microsoft is also adding partner plugins, with Enosix, Harvey, LSEG, Miro, monday.com, Moody’s, Morningstar, S&P Global Energy, and TeamsMaestro available now. Adobe, Atlassian, Box, Canva, Databricks, and others are coming soon.
    • With Nova enabled I am not seeing a difference with compactmode.show?
    • HOLY THREAD REVIVAL   But yes, look for browser.nova.enabled and set it to true
  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      522
    2. 2
      +Edouard
      196
    3. 3
      PsYcHoKiLLa
      110
    4. 4
      Steven P.
      90
    5. 5
      Nick H.
      71
  • Tell a friend

    Love Neowin? Tell a friend!