Page 1 of 1

Some Age Logic...

Posted: Wed Mar 09, 2005 10:03 pm
by s.dot
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

Posted: Wed Mar 09, 2005 10:10 pm
by feyd
your fields being their birthday? Add 13 to the year field. If it's in the future, they are too young. :)

Posted: Wed Mar 09, 2005 10:15 pm
by s.dot
aha I had no idea it was that simple


so..

Code: Select all

$check = $year + 13;
$currentyear = date(&quote;Y&quote;);

if($check > $currentyear){ echo &quote;TOO YOUNG!&quote;; }
?

Should take care of it all, even if their 13th birthday was like.... yesterday?

Posted: Wed Mar 09, 2005 10:23 pm
by feyd
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.

Code: Select all

date('Y/m/d');
...and you can use

Code: Select all

now

Posted: Thu Mar 10, 2005 6:07 pm
by s.dot
In theory this sounds good, but would it work?

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 }
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

and

Code: 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]

Posted: Thu Mar 10, 2005 6:38 pm
by feyd

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 &quote;\n&quote; . '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 &quote;\n&quote; . '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]

Posted: Thu Jun 16, 2005 6:40 am
by robster
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 &quote;\n&quote; . '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

Posted: Thu Jun 16, 2005 6:48 am
by robster
my fault, turns out rather than this:

Code: Select all

$DOB = "$get_bday_year-$get_bday_month-$get_bday_day";
I was typing this:

Code: Select all

$DOB = '$get_bday_year-$get_bday_month-$get_bday_day';
and that spacked out proceedings ;)


Nothing to see here... move along ;)

Posted: Thu Jun 16, 2005 8:43 am
by robster
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

Posted: Sat Jun 18, 2005 3:10 am
by robster
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.

Posted: Sat Jun 18, 2005 4:29 am
by wwwapu
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.
So it should work just fine.

Posted: Sat Jun 18, 2005 6:02 am
by robster
Thank you so much :)

Far out, what's with windows?! You couldn't use that in production. Anyway, it's not an issue as it's not a live server, just a testbed :)

Thanks again, I can continue in peace.