westman Posted February 23, 2012 Share Posted February 23, 2012 am looking to get my script working to find users over 18 years oldthis is what i have so faer... <?php// this will find users 7days old. how do i get 18 years?$age18 = date("Y-m-d",time() - (60*60*24*7));$sql = mysql_query("SELECT FROM user WHERE id='id' ");while($row = mysql_fetch_array($sql)){$dob = $row["dob"];}if (dob > $age18){$mess = "You are over 18";}else{$mess = "You are not over 18";} how do i get 18 year in "$age18"?and will my if statment work? Link to comment Share on other sites More sharing options...
thescientist Posted February 23, 2012 Share Posted February 23, 2012 if you can do basic math and googling you should be able to find the number of seconds in a year and multiply that times 18. make sure you put a $ in front of all your PHP variables. Link to comment Share on other sites More sharing options...
westman Posted February 23, 2012 Author Share Posted February 23, 2012 ok i know that...60 =second60 =minute24 =hour7 =day in$age18 = date("Y-m-d",time() - (60*60*24*7));but how do i get years (18 years)? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 23, 2012 Share Posted February 23, 2012 Figure out how many seconds there are in a day, and multiply by 365 to get the number of seconds in a year. Link to comment Share on other sites More sharing options...
thescientist Posted February 23, 2012 Share Posted February 23, 2012 https://www.google.com/#hl=en&gs_nf=1&tok=WvJ225KJKL0RGRPas9raaw&cp=9&gs_id=y&xhr=t&q=seconds+in+a+year&pf=p&output=search&sclient=psy-ab&pbx=1&oq=seconds+i&aq=0&aqi=g4&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=be4e7cd49a92f462&biw=1431&bih=779 * 18 Link to comment Share on other sites More sharing options...
boen_robot Posted February 23, 2012 Share Posted February 23, 2012 If you have PHP 5.3.0 or later, you can do it with something like: <?php$now = new DateTime;//Later, when you get the date as a string from the DBif ($now->diff(DateTime::createFromFormat('Y-m-d', $row['dob']))->y <= -18) {$mess = "You are over 18";}else{$mess = "You are not over 18";}?> This will account for any date quirks (e.g. leap years). Link to comment Share on other sites More sharing options...
westman Posted February 23, 2012 Author Share Posted February 23, 2012 i dont know if am running php 5.3 but i do know it is php V5am new at this, so here goes... $age18 = date("Y-m-d",time() - (60*60*24*365*18)); will this work? Link to comment Share on other sites More sharing options...
boen_robot Posted February 23, 2012 Share Posted February 23, 2012 You can find your PHP version by calling phpinfo() and looking at the very top of the output. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 23, 2012 Share Posted February 23, 2012 Instead of always posting and asking if something will work, why don't you just test it? You're getting the date, so print it out and see if it's 18 years ago. When people are at work writing code do you think we write a bunch of code and then go and find someone to show it to and ask if it will work, or do you think we just try it? Link to comment Share on other sites More sharing options...
Fordoloop Posted February 24, 2012 Share Posted February 24, 2012 http://www.google.ca...conds+in+a+year or even http://www.google.ca...nds+in+18+years Here's a demo to help you get all this under control: $age = date_parse("2001-01-01");echo "<br>The age in Y-M-D format is: ".$age[year]."-".$age[month]."-".$age[day]; $age_in_secs = mktime(0,0,0,$age[month],$age[day],$age[year]);echo "<br>The age in seconds is: ".$age_in_secs; $today_in_secs = date("U");echo "<br>The current date in seconds is: ".$today_in_secs; $difference_in_secs = ($today_in_secs - $age_in_secs);echo "<br>The difference between them in seconds is: ".$difference_in_secs; $difference_in_years = (((($difference_in_secs / 60)/60)/24)/365);echo "<br>The difference between them in years is: ".$difference_in_years; echo ($difference_in_years >= 18)?'<br>Age is over or equal to 18':'<br>Age is less than 18'; Just give that a run. If it throws a ton of errors, update your PHP. EDIT: Oops that was messy, hope it's easier to read now. Link to comment Share on other sites More sharing options...
thescientist Posted February 24, 2012 Share Posted February 24, 2012 i dont know if am running php 5.3 but i do know it is php V5am new at this, so here goes... $age18 = date("Y-m-d",time() - (60*60*24*365*18)); will this work? Maybe. Or you could just try it yourself and then you would know instead of asking each time and waiting for an answer to something you could have found out for yourself. Link to comment Share on other sites More sharing options...
westman Posted February 25, 2012 Author Share Posted February 25, 2012 thank you all for your time in the end i used...$age18 = date("Y-m-d",time() - (60*60*24*365*18)); am sorry for not testing my scriped before asking i will test my script more often. westman. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.