fireice87 Posted July 23, 2007 Share Posted July 23, 2007 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"); Quote Link to comment https://forums.phpfreaks.com/topic/61384-working-out-age-from-dob/ Share on other sites More sharing options...
Caesar Posted July 23, 2007 Share Posted July 23, 2007 What is the error you're actually getting/seeing? Quote Link to comment https://forums.phpfreaks.com/topic/61384-working-out-age-from-dob/#findComment-305516 Share on other sites More sharing options...
sasa Posted July 23, 2007 Share Posted July 23, 2007 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; ... Quote Link to comment https://forums.phpfreaks.com/topic/61384-working-out-age-from-dob/#findComment-305601 Share on other sites More sharing options...
Barand Posted July 23, 2007 Share Posted July 23, 2007 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/61384-working-out-age-from-dob/#findComment-305672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.