Jump to content

Recommended Posts

Hi Guys,

 

on my registration page users input there year,date and month of birth in 3 seperate drop down boxes say: [18] [June] [1979]

 

i store each value in Mysql as birthday,birthmonth and birthyear

 

what i was wanting to do is for PHP to work out this user is 27 years old and display age: 27 if you know what i mean not to sure how to acomplish this lol any help would be appreciated

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/47815-registration-age-query/
Share on other sites

Here's a working example.

<?php
$result = array('birthday' => 10, 'birthmonth' => 10, 'birthyear' => 1990);
$age = floor((time() - strtotime("{$result['birthday']}/{$result['birthmonth']}/{$result['birthyear']}")) / (365 * 24 * 60 * 60));
print $age;
?>

Simple and effective.

doesn't work 100%,

 

doesn't take months into account, ie if the month is the 4 and your birth month is the 7 then it will still count as if you had your birthday that year!

Just tested it and it worked as it should.  With 10/10/90 in there it showed an age of 16...According to what you said it should print 17.

$result = array('birthday' => 01, 'birthmonth' => 03, 'birthyear' => 1990);

returns

17

 

$result = array('birthday' => 01, 'birthmonth' => 05, 'birthyear' => 1990);

returns

17

 

the current Month is the 4

so on the 3rd month it should be 16 and on the 5th month it should be 17

<?php
function currentAge($birthday, $birthmonth, $birthyear) {
$dateParts = explode("-", date("m-d-Y"));
$birthDate = gregoriantojd($birthmonth, $birthday, $birthyear);
$todayDate = gregoriantojd($dateParts[0], $dateParts[1], $dateParts[2]);
return ($todayDate - $birthDate) / 365;
}
$result = array('birthday' => 1, 'birthmonth' => 04, 'birthyear' => 1990);
print currentAge($result['birthday'], $result['birthmonth'], $result['birthyear']);
?>

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.