Jump to content

working out age from DOB


fireice87

Recommended Posts

Hey iv got a script that works out the users age from there dob

$ageTime = mktime(0, 0, 0, 9, 8, 1987); // Get the person's birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;

echo 'You are ' . floor($ageYears) . ' years old.';

 

Im trying to expand this so that i can pull the DOB from the database and use variables to insert the DOB into the mktime( ). below is the code i have written to pull the dob from the database and add it too the calculation for the users age problem im having is that the age doesnt make it to the database.

the error i get is when it should be inserted into the database

 

iv used the code to get it into the database multiple times in diffrent scripts so i know thats fine so im thinking the error is something to do with the way im tring to use the variables in the calculation.

 

any help would be great as i cant work out the issue here... thanks  :)

$conn = @mysql_connect( "localhost", "username", "password")
or die ("could not connect to mysql");  #Connect to mysql
$rs = @mysql_select_db( "site23_thewu", $conn )
or die ("Could not select database"); #select database 

$sql0 = "DELETE 'age' FROM profile_quickfacts WHERE user= '$user'";

$empty=  $run= @mysql_query($sql0, $conn )
or die(" Could not add quick facts");

  $sql = "Select `day`,`month`,`year` FROM profile_quickfacts WHERE user= '$user' "; 

   $result= @mysql_query($sql, $conn )
or die(" Could not select data");

while($row = mysql_fetch_row($result))
{
$Day = $row[1];
$Month = $row[2];
$Year = $row[3];
} 

$ageTime = mktime(0, 0, 0, ($Day),  ($Month), ($Year)); // Get the person's birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;

// echo 'You are ' . floor($ageYears) . ' years old.';

$sql3 = "INSERT INTO profile_quickfacts(`age`)
VALUES (\"$ageYears\");";

    $run= @mysql_query($sql3, $conn )
or die(" Could not add quick facts");


Link to comment
Share on other sites

try

...
//$ageTime = mktime(0, 0, 0, ($Month),  ($Day), ($Year)); // Get the person's birthday timestamp
$ageYears = date('Y') - $Year;
$ageYears -= (date('m') * 100 + date('d')) < ($Month * 100 + $Day) ? 1 : 0;
//$t = time(); // Store current time for consistency
//$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
//$year = 60 * 60 * 24 * 365;
echo $ageYears;
...

Link to comment
Share on other sites

it doesn't have to be so complex

<?php
function calcAge($dob)
{
    $tob = strtotime($dob);
    $age = date('Y') - date('Y', $tob);    // age in years
    return (date('md') < date('md', $tob)) ? $age - 1 : $age;    // but if we haven't reached birthday this year, sub 1
}

echo calcAge ('1987-09-08');
?>

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.