Jump to content

users age isnt working right...


illusiveone

Recommended Posts

hi everyone

 

im trying to get this age property in my website to work properly, and the way it stands right now..

the age is being determined by the year and not by the specific date and month that the user supplies.

which isnt a big problem, but its annoying.here is the code that is being used atm.

 

<?php $dbt=$data['dob'];
$dbt2=explode('-',$dbt);
//print_r($dbt2);
$m=$dbt2[0];
$d=$dbt2[1];
$y=$dbt2[2];
$st=mktime(0,0,0,$m,$d,$y); 
$st1=date("F d, Y ",$st);
$date1=date("m, d, Y");
$year=$date1-$y;
?>

 

i havent used an age calculator such as this before, so im not too sure of myself when it comes to this. it also doesnt help that it was written by someone else. any and all help would be greatly appreciated, thank you in advance!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/88644-users-age-isnt-working-right/
Share on other sites

It's a simple calculation.

 

age = year now minus year born.

If the day/month of birth hasn't been reached yet this year then subtract 1 from age.

 

<?php
$dob = '1949-01-22';

echo age($dob);

function age($dob)
{
    $tob = strtotime($dob);
    $age = date('Y') - date('Y', $tob);
    return date('md') < date('md', $tob) ? $age-1 : $age;
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.