Some Age Logic...
Moderator: General Moderators
Some Age Logic...
Can someone give me some logic on how to check if a person is 13 or older.
I have fields for the age in the order of m/dd/yyyy
m = 1-12
d = 1-31
y = ex: 1999
I have fields for the age in the order of m/dd/yyyy
m = 1-12
d = 1-31
y = ex: 1999
aha I had no idea it was that simple
so..
?
Should take care of it all, even if their 13th birthday was like.... yesterday?
so..
Code: Select all
$check = $year + 13;
$currentyear = date("e;Y"e;);
if($check > $currentyear){ echo "e;TOO YOUNG!"e;; }Should take care of it all, even if their 13th birthday was like.... yesterday?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
well.. if you want to be specific about it, no.. you'd want to check against the entire date. I'd use unix timestamps or yyyy/mm/dd format for the comparison.. note: m and d would be the leading zero versions of the month and day, y would of course be the four digit year.
...and you can use
Code: Select all
date('Y/m/d');Code: Select all
nowIn theory this sounds good, but would it work?
of course it would be off by a few days, but that could be tweaked by giving the number of days in each month
feyd | Please use
Code: Select all
$day = 2073600;
$month = 66208000;
$year = 756864000;
$userday = $day2 * $day; // day2 = the users input of days (ex: 1, 2, 3)
$usermonth = $month2 * $month // same as above
$useryear = $year2 * $year; // same as above
$userageinseconds = $userday + $usermonth + $useryear;
$dateday = date('d');
$datedays = $dateday * $day;
$datemonth = date('m');
$datemonths= $datemonth * $month;
$dateyear = date('Y');
$dateyears = $dateyear * year;
$timenowinseconds = $datedays + $datemonths + $dateyears;
$allowedage = $timenowinseconds - ($year * 13);
if($userageinseconds > $allowedage){ echo "YOU'RE TOO YOUNG!"; } ELSE { //code }feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
<?php
// SQL date format: Y-m-d
$today = date('Y-m-d');
$DOB = '1996-03-15';
$adjusted = date('Y-m-d', strtotime($DOB . ' +13 year'));
if($today < $adjusted)
{
echo 'too young';
}
else
{
echo 'old enough';
}
echo "e;\n"e; . 'today is ' . $today . ', DOB is ' . $DOB . ', 13th birthday is ' . $adjusted;
?>[php="example 2: if you have the user's DOB in unix timestamp"]<?php
// SQL date format: Y-m-d
$today = time();
$DOB = strtotime('1996-03-15');
$adjusted = strtotime('+13 year', $DOB);
if($today < $adjusted)
{
echo 'too young';
}
else
{
echo 'old enough';
}
echo "e;\n"e; . 'today is ' . date('Y-m-d',$today) . ', DOB is ' . date('Y-m-d',$DOB) . ', 13th birthday is ' . date('Y-m-d',$adjusted);
?>
[syntax=php]too young
today is 2005-03-10, DOB is 1996-03-15, 13th birthday is 2009-03-15[/syntax]- robster
- Forum Contributor
- Posts: 360
- Joined: Wed Jul 16, 2003 8:28 am
- Location: Sunshine Coast, Australia
feyd wrote:Code: Select all
<?php // SQL date format: Y-m-d $today = date('Y-m-d'); $DOB = '1996-03-15'; $adjusted = date('Y-m-d', strtotime($DOB . ' +13 year')); if($today < $adjusted) { echo 'too young'; } else { echo 'old enough'; } echo "e;\n"e; . 'today is ' . $today . ', DOB is ' . $DOB . ', 13th birthday is ' . $adjusted; ?> [/quote] This is interesting, I'm trying the same thing so this thread came up in the search nicely. I'm unfortunately having to test on XP rather than OSX (which I'm used to) and when running a version of your code above, I get this interesting error: [syntax=php]Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in filename_changed.php[/syntax] I guess the big questions is, when I upload to my Linux live server, will it spack out on me also? or have I simply made a mistake and am not reading that error for what it really is (ie: telling me I screwed up my code)? Any advice greatefully taken :) Rob
- robster
- Forum Contributor
- Posts: 360
- Joined: Wed Jul 16, 2003 8:28 am
- Location: Sunshine Coast, Australia
my fault, turns out rather than this:
I was typing this:
and that spacked out proceedings 
Nothing to see here... move along
Code: Select all
$DOB = "$get_bday_year-$get_bday_month-$get_bday_day";Code: Select all
$DOB = '$get_bday_year-$get_bday_month-$get_bday_day';Nothing to see here... move along
- robster
- Forum Contributor
- Posts: 360
- Joined: Wed Jul 16, 2003 8:28 am
- Location: Sunshine Coast, Australia
I take it back, even though I fixed that error, windows still spacks if I choose a date in the 50's that pushes my 18+ date earlier than 1970.
I'm fine with this on my test server, but will I face this problem on my linux server when I send it live?
It's something I really need to sort out (seems weird to me, but mind you, a lot of things about windows does
).
Rob
I'm fine with this on my test server, but will I face this problem on my linux server when I send it live?
It's something I really need to sort out (seems weird to me, but mind you, a lot of things about windows does
Rob
- robster
- Forum Contributor
- Posts: 360
- Joined: Wed Jul 16, 2003 8:28 am
- Location: Sunshine Coast, Australia
Hiya, just thought I'd ask again, hope nobody minds. I can accept this dodgy windows server can only accept a date from 1970 onwards, but will my linux live server do the same thing (when I finally get it ready for upload and testing)?
I can't imagine how the web could exist with such a fundamental flaw/restriction.
I can't imagine how the web could exist with such a fundamental flaw/restriction.
So it should work just fine.The big Manual on function date() wrote:The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). On Windows this range is limited from 01-01-1970 to 19-01-2038.