BuildMyWeb Posted May 7, 2014 Share Posted May 7, 2014 so i have been asked to verify that someone is 21 according to the dob entered in an HTML FRI form. basically we are accepting data in text input fields. two digits for month, two for day, and 4 for year. the data is then formatted as per the following: "mm/dd/yy" via concatenation. in our handler script, we are doing the following: $dob = $arr_m[$i] . "/" . $arr_d[$i] . "/" . $arr_y[$i]; $dob_ts = strtotime($dob); $today_ts = time(); $twentyone = 21*365*24*60*60; if( ($today_ts - $twentyone) > $dob_ts ) echo"we are 21"; our results are not what we expect tho. today's date is may 6 2014. however our script is accepting 05/11/1993 as being 21 years old and not 05/12/1993. we are off by a few days. i assume there is a better way to calculate 21 years of age from today's date? Quote Link to comment https://forums.phpfreaks.com/topic/288298-comparing-dates-timestamps/ Share on other sites More sharing options...
mac_gyver Posted May 7, 2014 Share Posted May 7, 2014 the age of something is the difference in years between the current year and the year of birth, subtract one if the birthday (month/day) hasn't occurred yet in the current year. an example - $dob = '1993-05-12'; // standard yyyy-mm-dd format $today = date("Y-m-d"); $age = substr($today, 0, 4) - substr($dob, 0, 4) - (substr($today, 5,5) < substr($dob, 5,5)); Quote Link to comment https://forums.phpfreaks.com/topic/288298-comparing-dates-timestamps/#findComment-1478508 Share on other sites More sharing options...
Jacques1 Posted May 8, 2014 Share Posted May 8, 2014 Using the date functions of PHP isn't challenging enough for you guys, I guess? <?php // check the manual for the acceptable date formats $birthdate = '1993-05-06'; if (strtotime($birthdate) <= strtotime('21 years ago')) { echo 'You are indeed 21.'; } else { echo 'You are not old enough.'; } Quote Link to comment https://forums.phpfreaks.com/topic/288298-comparing-dates-timestamps/#findComment-1478721 Share on other sites More sharing options...
Solution BuildMyWeb Posted May 8, 2014 Author Solution Share Posted May 8, 2014 (edited) thx guys Edited May 8, 2014 by BuildMyWeb Quote Link to comment https://forums.phpfreaks.com/topic/288298-comparing-dates-timestamps/#findComment-1478806 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.