Vivid Lust Posted February 17, 2008 Share Posted February 17, 2008 This code will not work properly, please help! $dob = 28-08-92 $today = date("d-m-Y"); $age= $dob-$today; When echoed $today it displays 11 which is wrong. Please help! Quote Link to comment https://forums.phpfreaks.com/topic/91573-php-date-help/ Share on other sites More sharing options...
wildteen88 Posted February 17, 2008 Share Posted February 17, 2008 Could you explain what are you wanting to do with the code? Get the persons age? Quote Link to comment https://forums.phpfreaks.com/topic/91573-php-date-help/#findComment-469031 Share on other sites More sharing options...
Vivid Lust Posted February 17, 2008 Author Share Posted February 17, 2008 yes, get a persons age. Quote Link to comment https://forums.phpfreaks.com/topic/91573-php-date-help/#findComment-469035 Share on other sites More sharing options...
wildteen88 Posted February 17, 2008 Share Posted February 17, 2008 Just subtract their brith year from the current year, eg: $dob = '28-08-92'; $birthYear = date('Y', strtotime($dob)); $curYear = date("Y"); $age = $curYear - $birthYear; echo 'Your age is: ' . $age . ' years old'; Quote Link to comment https://forums.phpfreaks.com/topic/91573-php-date-help/#findComment-469041 Share on other sites More sharing options...
Vivid Lust Posted February 17, 2008 Author Share Posted February 17, 2008 Could you modify to include month and day? So that the age is accurate? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/91573-php-date-help/#findComment-469059 Share on other sites More sharing options...
spfoonnewb Posted February 17, 2008 Share Posted February 17, 2008 <?php function birthday ($birthday) { list($year,$month,$day) = explode("-",$birthday); $year_diff = date("Y") - $year; $month_diff = date("m") - $month; $day_diff = date("d") - $day; if ($month_diff < 0) $year_diff--; elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--; return $year_diff; } echo birthday("1980-07-02"); ?> http://snipplr.com/view/1357/calculate-age/ http://www.google.com/search?hl=en&q=php+get+age&btnG=Search Quote Link to comment https://forums.phpfreaks.com/topic/91573-php-date-help/#findComment-469063 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.