Jump to content

Recommended Posts

<?php
$birthday = split('/', $birth);
$years = date('Y') - $birthday[2];
if (birthday[1] > date('m'))
{
     $years++;
} elseif ((birthday[1] == date('n')) && (birthday[0] >= date('j')))
{
     $years++;
}
?>

 

Untested, and might be an easier way, but that should work.

   

Link to comment
https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787018
Share on other sites

The best way is to perform the calculation straight in the query, as PFMaBiSmAd displayed.

 

If you are going to do it from PHP (which is pointless if you're grabbing the dates from the DB) you can use something much simpler:

 

floor((time() - strtotime($birthday))/31556926);

Link to comment
https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787026
Share on other sites

The best way is to perform the calculation straight in the query, as PFMaBiSmAd displayed.

 

If you are going to do it from PHP (which is pointless if you're grabbing the dates from the DB) you can use something much simpler:

 

floor((time() - strtotime($birthday))/31556926);

 

thats the only one that actually worked for me and it was a year out :( i kept getting

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5)

 

with the query method :S

Link to comment
https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787058
Share on other sites

The subtraction method does not work because the definition of the age of anything is the difference in years, subtract one if the birthday has not yet occurred in the current year. The is what the equation at the link I posted does and it can be ported in php code if you need.

 

Cannot help you with your sql syntax error unless you post the actual code that generated the error.

Link to comment
https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787069
Share on other sites

$query="select user_dob, CURDATE(), (YEAR(CURDATE(),5)<RIGHT(user_dob)) AS age from user_accounts where user_id='$user_id'";

$result=mysql_query($query);

if (!$result)

{

die(mysql_error());

}

$num_rows=mysql_num_rows($result);

 

$row=mysql_fetch_array($result); 

$age = ($row['age']);

Link to comment
https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787084
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.