Jump to content

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;
}

?>

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.