fohanlon Posted March 7, 2006 Share Posted March 7, 2006 HiI need to be able to calculate the exact age of a person based on a comparison to todays date.I have tried to use timestamp but I run into the 1970 problem. The dob the user can choose can go from 1st jan 1910.For example:Today is the 7th march 2006Using the following dates I need to be able to get the results below:DOB: 6th March 1988 - return whether 18 or not DOB: 7th March 1988 - return whether 18 or notDOB: 8th March 1988 - return whether 18 or notI have searched the web but all code rounds to nearest year but I need to be exact as the solution will determine a calculated cost.Any help would be greatly appreciated.Thanks,Fergal. Quote Link to comment https://forums.phpfreaks.com/topic/4322-date-difference-problem/ Share on other sites More sharing options...
Toni_montana Posted March 7, 2006 Share Posted March 7, 2006 In my opinion, you don't need the exact age to tell wheter someon is above 18 or not.Just use one of the solutions you found, and instead of round() use floor().This will round the age down.good luckrogier[!--quoteo(post=352475:date=Mar 7 2006, 07:47 AM:name=fohanlon)--][div class=\'quotetop\']QUOTE(fohanlon @ Mar 7 2006, 07:47 AM) [snapback]352475[/snapback][/div][div class=\'quotemain\'][!--quotec--]HiI need to be able to calculate the exact age of a person based on a comparison to todays date.I have tried to use timestamp but I run into the 1970 problem. The dob the user can choose can go from 1st jan 1910.For example:Today is the 7th march 2006Using the following dates I need to be able to get the results below:DOB: 6th March 1988 - return whether 18 or not DOB: 7th March 1988 - return whether 18 or notDOB: 8th March 1988 - return whether 18 or notI have searched the web but all code rounds to nearest year but I need to be exact as the solution will determine a calculated cost.Any help would be greatly appreciated.Thanks,Fergal.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/4322-date-difference-problem/#findComment-15040 Share on other sites More sharing options...
txmedic03 Posted March 7, 2006 Share Posted March 7, 2006 If all you want to know is the age in years then exact age is not important, but I think I have a script that will fit your need.Assuming the following code is in age.phpdate should be passed as age.php?date=6/5/1988You can change the way it gets the date to suit your needs, but the basic idea is here.This is just what I came up with off the top of my head. There are probably a handful of ways to do it.[code]<?php$arr1 = explode("/",$_GET['date']);$arr2 = array(date("m"),date("d"),date("Y"));$year = $arr2[2] - $arr1[2];if ( ( $arr1[0] > $arr2[0] ) || ( ( $arr1[0] = $arr2[0] ) && ( $arr1[1] < $arr2[1] ) ) ) $year = $year - 1;echo "<p>User born on ".$_GET['date']." is ".$year." years old.</p>\r\n";if ( $year < 21 ) echo "<p>User is under 21.</p>\r\n";if ( $year < 18 ) echo "<p>User is a minor.</p>\r\n";?>[/code]I hope this helps.Happy coding! Quote Link to comment https://forums.phpfreaks.com/topic/4322-date-difference-problem/#findComment-15093 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.