Some Age Logic...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Some Age Logic...

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your fields being their birthday? Add 13 to the year field. If it's in the future, they are too young. :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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]
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post 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
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post 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 ;)
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post 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
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post 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.
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post 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.
Post Reply