ki Posted July 1, 2007 Share Posted July 1, 2007 How can I determine someones exact age in years without doing date("Y") - $birthyear? Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/ Share on other sites More sharing options...
AndyB Posted July 1, 2007 Share Posted July 1, 2007 By telephone, email, or telepathy perhaps. What's the real question in context? Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287315 Share on other sites More sharing options...
ki Posted July 1, 2007 Author Share Posted July 1, 2007 well when a user enters hos exact birth month day and year i want to know how i can determine his exact age in years Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287328 Share on other sites More sharing options...
MadTechie Posted July 1, 2007 Share Posted July 1, 2007 <?php $ageTime = mktime(0, 0, 0, 7, 3, 1978); // 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.'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287329 Share on other sites More sharing options...
AndyB Posted July 1, 2007 Share Posted July 1, 2007 Caution #1: mktime only works for dates beyond Jan 1/1970 - http://ca.php.net/manual/en/function.mktime.php Caution #2: some years have 366 days Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287338 Share on other sites More sharing options...
ki Posted July 1, 2007 Author Share Posted July 1, 2007 well, is there another way instead of mktime? Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287342 Share on other sites More sharing options...
Barand Posted July 1, 2007 Share Posted July 1, 2007 try this. Subtract the years but if the actual birth date has not yet been reached this year then subtract 1 <?php function getAge ($dob) { $tob = strtotime($dob); $age = date('Y') - date('Y', $tob); return (date('md') < date('md', $tob)) ? $age-1 : $age; } echo getAge ('1949-01-22').'<br>'; //58 echo getAge ('1945-12-16').'<br>'; //61 ?> Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287349 Share on other sites More sharing options...
xyn Posted July 1, 2007 Share Posted July 1, 2007 Simelar to Barand's answer. Thouhg you must input the entire Date Of Birth.. DD/MM/YYYY function Getage($str) { $dob = explode("/", $str); $year = $dob[2]; $month = $dob[1]; $day = $dob[0]; $year_to = date("Y"); $month_to = date("m"); $day_to = date("d"); $age_this_year = $year_to - $year; $age_minus_one = $age_this_year - 1; if($month_to == $month) { if ($day_to >= $day) { $age = $age_this_year; } else { $age = $age_minus_one; } } else { if($month_to > $month) { $age = $age_this_year; } else { $age = $age_minus_one; } } return $age; } Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287353 Share on other sites More sharing options...
ki Posted July 1, 2007 Author Share Posted July 1, 2007 thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287357 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.