downah Posted March 13, 2012 Share Posted March 13, 2012 Hi guys, My apologies if this is in the wrong forum but I am not really sure how to go about this. I have not written any code for this but I have four fields in one table one called age and 3 others dobmonth / dobday /dobyear - My question being how would I write some code that automatically fills in the age field based on the date of birth fields? If anyone could point me in the right direction that would be awesome, Appreciated. Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/ Share on other sites More sharing options...
floridaflatlander Posted March 13, 2012 Share Posted March 13, 2012 They use to have a good tutorial here at freaks, the link is somewhere on the front page or there about Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/#findComment-1326880 Share on other sites More sharing options...
scootstah Posted March 13, 2012 Share Posted March 13, 2012 Well for one, you should only have one database column for date of birth. Make it of DATE type. This way you can easily work with the data and it makes queries easier. Secondly, what does your age field look like? Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/#findComment-1326889 Share on other sites More sharing options...
downah Posted March 13, 2012 Author Share Posted March 13, 2012 I was using a date before but does it not require users to use dd/mm/yyyy ? I now have 3 selects which nicely lets user select the month day and year, is this possible to put into one date field? if so enlighten me! Back to the main question my age field at the moment is in the same table as the dob fields and it is an int. - or is this not what you meant? to floridaflatlander Is this tutorial about this certain question or similar? if so I will have a look Thanks for the help so far guys! Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/#findComment-1326900 Share on other sites More sharing options...
litebearer Posted March 13, 2012 Share Posted March 13, 2012 You can concatenate the month, day, year and separators to create a variable to insert into your table date field. Then you can use http://www.zoomcities.com/forum/showthread.php?tid=5728 Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/#findComment-1326915 Share on other sites More sharing options...
floridaflatlander Posted March 13, 2012 Share Posted March 13, 2012 Is this tutorial about this certain question or similar? if so I will have a look Similar http://www.phpfreaks.com/tutorial/working-with-dates-in-php Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/#findComment-1326917 Share on other sites More sharing options...
RobertP Posted March 13, 2012 Share Posted March 13, 2012 function getAgeFromTime($time,$today = null){ if(!$today) $today = time(); $age = date('Y',$today)-date('Y',$time); if(date('m.d',$today)<=date('m.d',$time)) $age--; return $age; } Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/#findComment-1326933 Share on other sites More sharing options...
downah Posted March 13, 2012 Author Share Posted March 13, 2012 Thanks a lot for the timediff() I think that will help, and it makes sense to store the 3 selects into one variable and to send that through to the server, thanks for the input! to robertP I keep reading over the code you wrote and understand it considerably but would it be able for you to explain me how to use that in action? Appreciated Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/#findComment-1326943 Share on other sites More sharing options...
downah Posted March 14, 2012 Author Share Posted March 14, 2012 You can concatenate the month, day, year and separators to create a variable to insert into your table date field. Then you can use http://www.zoomcities.com/forum/showthread.php?tid=5728 I have now concatenated the 3 fields into one variable which goes into a date field (dob) Link to comment https://forums.phpfreaks.com/topic/258839-age-date-of-birth/#findComment-1327069 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.