18+ check

Recommended Posts

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?

Share on other sites

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.

Share on other sites

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)?

Share on other sites

Figure out how many seconds there are in a day, and multiply by 365 to get the number of seconds in a year.

Share on other sites

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).

Share on other sites

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?

Share on other sites

You can find your PHP version by calling phpinfo() and looking at the very top of the output.

Share on other sites

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?

Share on other sites

` \$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.

Edited by Fordoloop
Share on other sites

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.
Share on other sites

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.