• 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

    • It depends, but it's also going to push more adoption of open source AI models that can run locally, I'm seeing a pattern among the US and China when it comes to closed and open source AI models, they close when they feel they have the advantage, open up when they feel they don't. This has little to do with security and everything to do with control, the irony is that the ones that do go open and get the community behind them are the ones that have the real control, hence why we keep seeing this seesaw from closed to open. Ultimately, it's far too dangerous to let any one country or corporation to control AI, which is why open source AI or AI you can run locally on your hardware is very likely going to be the future of AI over the long run, this is especially the case as AI becomes more powerful and useful for user, privacy, security and censorship will become more of an issue for users and businesses.
    • A person should only be required to click through to an article if they want to know more details. The headline should give basic facts. A basic fact would include the name of the service being shut down.
    • That would probably be a good outcome, but it would also require other "features" be changed to comply, such as Auto play, scrolling, and advertising. Having the laws shift these platforms back to a socially responsible and useful places is a benefit for everyone. The addictive nature is the problem, and while many blame the individual, psychologists would suggest the platform is using a well trodden playbook to hook in more revenue..
    • It is in fact being discontinued in the US in July.
    • It was a rather nice rant as I rather dislike when people support the shitifcation of the Internet. My comment also factually applied the definition of "clickbait' to this headline unlike your comment. I have been reading this site for some 20 years now so I will call them out when they are doing carp they shouldn't be doing but thanks for your concern. You can't even do a search by headline anymore to do a quick search to find something because most of the titles lack all the relevant keywords that should be there.
  • 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
      523
    2. 2
      +Edouard
      207
    3. 3
      PsYcHoKiLLa
      114
    4. 4
      Steven P.
      90
    5. 5
      Nick H.
      71
  • Tell a friend

    Love Neowin? Tell a friend!