Jump to content

[SOLVED] Work out age from date of birth.


DeanWhitehouse

Recommended Posts

@rajivgonsalves

I used your code

$day = trim(htmlentities($_POST['age']));
$month =trim(htmlentities($_POST['age1']));
$year =trim(htmlentities($_POST['age2']));
$dob = $day.$month.$year;

//Work out birthday
$strDate = $dob;
echo floor((time()-strtotime(substr($strDate,4,4)."-".substr($strDate,2,2)."-".substr($strDate,0,2)))/(86400*365));
//end bday

 

but this works for my birthday and probably more, but when i submitted a form with the birthday being

01011920

it got the age 38.

 

any ideas?

Link to comment
Share on other sites

You just need years, right?

 

Different solution of the one earlier presented.  In my opinion, this one is cleaner.  Not sure if it's more efficient.

 

<?php

$month = 2;
$day = 18;
$year = 1992;

$tyear = (int) date('Y');
$tmonth = (int) date('n');
$tday = (int) date('j');
$years = $tyear - $year - 1;
if($tmonth > $month || ($tmonth == $month && $tday >= $day)) {
//oddly written, but i think that's the most effecient way to do it.
++$years;
}
//if the current date is past (or equal to) the birthday, add a year

echo $years;

?>

 

 

Edit:  Bleh, just realized the "if($month <= date('n') && $day <= date('j')) ++$years;" part is wrong.  Since you already have a solution, I'm not gonna bother fixing it, unless you're interested in this.

 

 

Edit:  Fixed it, because it bothered me not having it correct. lol

Link to comment
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.