• 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

    • RustDesk 1.4.8 by Razvan Serea RustDesk is a fast, secure, and open-source remote desktop software designed for self-hosting, remote access, and IT support. It provides a privacy-focused alternative to TeamViewer and AnyDesk, offering full control over your data with minimal configuration. The client is fully open source, while users have the option to choose between two server solutions: the Professional Server, a premium offering with advanced features available for purchase, and the Basic Server, a free and open-source alternative for those who prefer a self-hosted setup. RustDesk features Open-source & free remote desktop solution Cross-platform compatibility – Works on Windows, macOS, Linux, iOS, Android, and Web End-to-end encryption (E2EE) based on NaCl for secure connections Peer-to-peer (P2P) connectivity for fast and private remote access Self-hosting support – Own your data with easy deployment on your infrastructure Supports VP8, VP9, AV1 (software) and H264, H265 (hardware) codecs for efficient streaming Unattended access for remote management File transfer & clipboard sharing Multi-monitor support & remote printing Low-latency & high-performance remote access Session recording & chat functionality Professional & Basic server options for flexible deployment Lightweight & minimal resource usage No third-party server dependency for privacy No installation or admin privileges needed on Windows (elevate privileges locally or remotely on demand) Easy installation & minimal configuration required Custom branding & enterprise-level features available RustDesk 1.4.8 changelog: Added Add Windows arm64 support #15139 Feature: Add monitor-switch buttons to remote toolbars #15342 Refact/privacy mode 1 multi monitors #15321 autocomplete online #15313 feat: theme logo #15268 Changes refact: restart remote device, autoconnect #15290 refact(oidc): icon azure to microsoft #15278 Refact/printer driver default unchecked #15191 Revert "fix(iPad): keep touch gestures with external mouse" #15288 Fixes fix Wayland→Wayland clipboard paste fix(arm64-linux): fix CJK font rendering on flutter-elinux #15324 iOS: autocorrect/data detectors corrupt the server Key field (ID/Relay Server settings), making valid keys impossible (or very hard) to enter #15293 fix(ios): mouse mismatch #15339 fix(linux): reap leftover logind session procs on headless teardown #15337 Crash on startup (0xc0000409) / Fast Fail in librustdesk.dll on Windows 11 26H1 #15218 fix(clipboard): Windows DIB images, fill missing alpha #15296 Fix/generate py target injection #15248 Fix clipboard synchronization not fully disabled in View Only mode #15224 fix(keyboard): win, key, Pause #15351 Download: RustDesk 64-bit | MSI | 32-bit ~20.0 MB (Open Source) Links: RustDesk Home Page | Other platforms | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • This makes me think of Dune for some reason.
    • I'm too old to return to the "good old days" when I was installing custom ROMs and tinkering with my devices - now I just want to turn it on and use it. I've read that banking and payment apps work on Murena /e/OS (I'll have to check the ones I use) and I also really want to support Fairphone 😉
    • Time to start going to the local church and play Bingo for a while.
  • Recent Achievements

    • First Post
      mike_rumble earned a badge
      First Post
    • Dedicated
      tuben earned a badge
      Dedicated
    • Week One Done
      mnsgroup earned a badge
      Week One Done
    • Conversation Starter
      sumytbe earned a badge
      Conversation Starter
    • One Year In
      B4dM1k3 earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      203
    3. 3
      PsYcHoKiLLa
      98
    4. 4
      Michael Scrip
      80
    5. 5
      neufuse
      67
  • Tell a friend

    Love Neowin? Tell a friend!