• 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

    • Threads scales past half a billion users, brings deeper community and feed controls by Fiza Ali Meta has announced Threads crossing a major milestone of 500 million monthly active users. And, at the heart of this growth sits something simple: communities. From books to basketball, parenting to music, Threads says its rise has been powered by people clustering around shared interests and, in turn, giving the platform its identity. In response, the platform is expanding its Communities feature beyond beta and introducing a set of new tools designed to make participation easier and more engaging. A redesigned Communities Hub will now appear in the main navigation menu, allowing users to jump between groups without leaving their feed. Each community will also receive a distinct Community Icon, giving them clearer visual identity and making them easier to recognise across the platform. Then there’s Community Progress, which is a kind of live gauge showing how close a topic is to becoming a full-fledged community, alongside guidance on how users can contribute to its development. In addition, Meta is also expanding its Community Champions programme, recognising more users who actively contribute to community engagement. And then things go more local; Local Communities is already available in 100 countries, including North America, South America, Asia, and Europe but are now rolling out with native-language tags starting in Japan, South Korea, and Taiwan. The platform is also expanding Live Chats to more communities in the coming weeks, adding features such as co-hosting and the ability to quote moments directly into users’ feeds. Beyond communities, Meta is tightening the loop between users and their feeds. Earlier this year came "Dear Algo," a feature that lets people tell Threads what they want more or less of. Now it’s being paired with a new tool, "Your Algo." It allows people to adjust how frequently certain topics appear, with options lasting one, three, or seven days. Meta says these preferences remain private and can be managed alongside “Dear Algo” in a unified settings hub. The rollout begins in the US, Canada, UK, Australia, and New Zealand. Finally, the company says these changes are part of an ongoing effort to refine Threads based on user feedback and that further updates will continue as the platform evolves.
    • You pay just $100 per TB with this rare 4TB PCIe Gen4 NVMe SSD deal by Sayan Sen SSDs and GPUs are incredibly hard to get nowadays due to high pricing. Discounts are quite rare which is why we report on them as soon as we spot a good deal. For example AMD's new 9070 GRE was finally up for sale at a very good price of just $500 thanks to a special coupon. Sadly that deal is gone but if you happen to be looking for a 4TB NVMe SSD and can spend around $400 there is a really good offer on sale that you should not miss out on as TeamGroup's 4TB G50 model is on sale for that that price which means you are only paying $100 per TB, a very good deal in the current market (purchase link under the specs table down below). The TeamGroup T-FORCE G50 NVMe SSD is a PCIe Gen4 drive and as such it promises to deliver sequential read speeds of up to 5,000 MB/s, helping accelerate game loading, file transfers, and everyday computing tasks. Since this is a 4TB drive you can use it for a gaming library to take advantage of things like DirectStorage. The SSD features an InnoGrit controller and SLC caching technology to support consistent performance. An ultra-thin, patented graphene heatsink is included to aid in heat dissipation. Get it at the link below: Team Group T-FORCE G50 4TB Internal SSD (TM8FFE004T0C129): $449.99 + $50 off w/ promo code SSF69668, limited offer => $39.99 (Sold and Shipped by Newegg US) 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.
    • I agree. I also think Phil stayed too long. They should definitely fire whoever thought all a console platform needed was Call of Duty, Elder Scrolls, and Fallout to survive. Asha and crew are still saying they need more Elder Scrolls and Fallout games. They simply don't get it.
  • 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
      200
    3. 3
      PsYcHoKiLLa
      110
    4. 4
      Steven P.
      89
    5. 5
      Nick H.
      71
  • Tell a friend

    Love Neowin? Tell a friend!