chanfuterboy Posted September 16, 2009 Share Posted September 16, 2009 hi, in register user put a date a month and a year. so i want that in profile it show the age exact. e.g 14-12-2007 so in profile it need to come age 2 i have written a code here but it show online the whole date <?php echo floor(gregoriantojd(date('m'),date('d'),date('y'))-gregoriantojd(date('m',strtotime($row['DateOfBirth'])),date('d',strtotime($row['DateOfBirth'])),date('y',strtotime($row['DateOfBirth'])))/365);?> can someone help my out Link to comment https://forums.phpfreaks.com/topic/174461-solved-telling-age/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2009 Share Posted September 16, 2009 A) You should always store dates using a DATE data type YYYY-MM-DD. B) Once you do that, you can calculate the age directly in a query. See the example at this link - http://dev.mysql.com/doc/refman/5.1/en/date-calculations.html Link to comment https://forums.phpfreaks.com/topic/174461-solved-telling-age/#findComment-919506 Share on other sites More sharing options...
TeNDoLLA Posted September 16, 2009 Share Posted September 16, 2009 I am not sure if you tried to calculate the age of a user based on the birth date they entered, but if so and you want to do it in php's side you could do it also with DateTime class (required php >= 5.3 though). <?php // Create a new DateTime object with the user entered date. $birthDate = new DateTime('1983-03-21 12:00:00'); // Calculate difference to today. $diff = $birthDate->diff(new DateTime()); // Echo age. echo $diff->y . ' years, ' . $diff->m . ' months, ' . $diff->d . ' days'; Link to comment https://forums.phpfreaks.com/topic/174461-solved-telling-age/#findComment-919550 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.