/    Sign up×
Community /Pin to ProfileBookmark

php myql age

HI

I’m trying to calculate a persons age. Their date of birth is stored in a mysql table (column is set as date) format yyyy-m-d

how would I calculate their age based on todays date

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@The_Little_GuyAug 29.2006 — get the current year, then subtract the two years. that will get there age.
Copy linkTweet thisAlerts:
@kprocauthorAug 30.2006 — Below is a function that I fould which is suppose to calculate a persons age. The persons date of birth comes from a mysql database (column set as Date) in the format yyyy-mm-d. When the mysql column was set a VARCHAR and I entered the date as MM/DD/YYYY the below worked.

and ideas why this is not working

the function
[code=php]
<?
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}

?>

[/code]



calculating the age
[code=php]
$spouseage = dateDiff("-", date("Y-m-d"), $spousedob);
[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyAug 30.2006 — [code=php]<?
$currentDate = date('Y-m-d');
$birthDate = "1986-07-22";

$age = $currentDate - $birthDate;

echo $age;

?>[/code]


for the var $birthDate you would set that to what is in the database, or where ever your getting the source from.
Copy linkTweet thisAlerts:
@kprocauthorAug 30.2006 — As you can tell I'm by far not an expert at this. I found the function I posted on by seaching google. why would someone use such a compliated proccess when you can do it as you posted above
Copy linkTweet thisAlerts:
@The_Little_GuyAug 30.2006 — IDK, but what I did works for me, does it work for you?
Copy linkTweet thisAlerts:
@kprocauthorAug 30.2006 — Like a charm, thank you very much
Copy linkTweet thisAlerts:
@NogDogAug 30.2006 — [code=php]<?
$currentDate = date('Y-m-d');
$birthDate = "1986-07-22";

$age = $currentDate - $birthDate;

echo $age;

?>[/code]


for the var $birthDate you would set that to what is in the database, or where ever your getting the source from.[/QUOTE]

Try that with at birthdate of 1986-12-31, and you'll find that it gives the same age, even though the person hasn't had his/her birthday yet for this year. That is what the more complicated function you were looking at is trying to deal with.

(The above function is actually just subtracting the year portions of each value, as anything in the strings from the first non-numeric character (the hyphen) will be ignored when treating strings as numbers.)
Copy linkTweet thisAlerts:
@kprocauthorAug 30.2006 — this brings me back to the function, any thoughts why its not working. the result it returns is 0
Copy linkTweet thisAlerts:
@NogDogAug 30.2006 — The gregoriantojd() function wants its arguments in this order: month, day, year. So if your dates are in y-m-d (which is much better, in my opinion), then you'll need to change these two lines:
[code=php]
$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
[/code]
Copy linkTweet thisAlerts:
@kprocauthorAug 30.2006 — Thanks for the help everyone. everything is working excellent
×

Success!

Help @kproc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...