Calculate age based on date of birth

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Post by Verminox »

Oren wrote:I run PHP 5.1.4 and when I entered a date before 1.1.1970 the function returned 36, so I guess the negative timestamps don't work after all.

I run PHP 5.1.2 and it works well with dates upto 1901. It seems to screw up with 1900 though and I can't figure out why.

Let me go lookup this peculiar behaviour then... :roll:

Edit: Just found it on the strtotime() manual.
Note: 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.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

I have PHP 5.1.2 on Windows XP Professional running Apache 1.3 and negative timestamps work for me upto 13 Dec 1901 :)[/url]
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Verminox wrote:I run PHP 5.1.2 and it works well with dates upto 1901. It seems to screw up with 1900 though and I can't figure out why.

Let me go lookup this peculiar behaviour then... :roll:

Edit: Just found it on the strtotime() manual.
Note: 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.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

I have PHP 5.1.2 on Windows XP Professional running Apache 1.3 and negative timestamps work for me upto 13 Dec 1901 :)
That's strange, dates before 1.1.1970 shouldn't work on a windows system.
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Post by Verminox »

Well unless somebody has secrectly installed a Linux distribution on my machine and made it look like WinXP I think it should work, actually.

And it says on php.net that the issues was fixed after PHP 5.1.0, so there we go.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Verminox wrote:Well unless somebody has secrectly installed a Linux distribution on my machine and made it look like WinXP I think it should work, actually.

And it says on php.net that the issues was fixed after PHP 5.1.0, so there we go.
I don't know, it didn't work for me and I run PHP 5.1.4 :cry:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

~Verminox is your system 32 bit or 64 bit?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You'll need to set it far enough back because the epoch is based on GMT .. so at minimum you need to adjust the numbers for your timezone.

For instance:

Code: Select all

[feyd@home]>php -r "echo date('Y-m-d H:i:s', -1 * 120 * 24 * 3600);"
1969-09-02 19:00:00
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Post by Verminox »

d11wtq wrote:~Verminox is your system 32 bit or 64 bit?
32-Bit


I can copy my phpinfo() page somewhere else and show you if you want to know know the configuration settings.

I have no clue how it is working on my machine o_O

Edit: Oren, you said you tried a date before 1.1.190. Did you try 1969 because then it would return 36 even after taking a negative timestamp which you might have overseen. Try with a date such as "1 Jan 1940" and check if it returns 66.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Something very strange is going on here... I had tested my function again and for 1.1.1960 it gave me 36 and for 1.1.1940 it gave me 0.
I then ran another test and all of a sudden it worked even with dates like 1.1.1902.
This is very strange, anyone has any ideas?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Perhaps it has something to do with whether the variable contains a string or integer?
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Post by Verminox »

Oren wrote:Something very strange is going on here... I had tested my function again and for 1.1.1960 it gave me 36 and for 1.1.1940 it gave me 0.
I then ran another test and all of a sudden it worked even with dates like 1.1.1902.
This is very strange, anyone has any ideas?
Are you using the same function you posted earlier?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

astions wrote:Perhaps it has something to do with whether the variable contains a string or integer?
I didn't use integers, only strings.
Verminox wrote:Are you using the same function you posted earlier?
Yes.
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Post by Verminox »

I tried your function on my PC. Here goes:

Code: Select all

<?php
function get_age($age){
	$diff = time() - strtotime($age);
	return (int) ($diff / (3600 * 24 * 365));
} 

$dates = array(
	'1.1.2006',
	'1.1.2000',
	'1.1.1970',
	'1.1.1940',
	'1.1.1902',
	'1.1.1901',
	'1.1.2010',
	'1.1.2039'
	);
foreach ($dates as $i => $date){
	echo $i." => ".get_age($date)."\n";
}
?>
Output:

Code: Select all

0 => 0
1 => 6
2 => 36
3 => 66
4 => 104
5 => 36
6 => -3
7 => 36

5 => A unix timestamp cannot be made for a date prior to 13 Dec 1901 since it is the maximum negative value a 32-bit signed integer can take. Hence, the strtotime() function returns 0 which makes the age 36 (counting the DOB to be 1.1.1970).

6 => Works as expected

7 => Same as (5), but this time the max value in the postive side.


Doesn't look like a problem to me on WinXP Professional/Apache 1.3/PHP 5.1.2
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Verminox wrote:Doesn't look like a problem to me on WinXP Professional/Apache 1.3/PHP 5.1.2
I tried your code exactly as it is:

Code: Select all

<?php
function get_age($age){
        $diff = time() - strtotime($age);
        return (int) ($diff / (3600 * 24 * 365));
}

$dates = array(
        '1.1.2006',
        '1.1.2000',
        '1.1.1970',
        '1.1.1940',
        '1.1.1902',
        '1.1.1901',
        '1.1.2010',
        '1.1.2039'
        );
foreach ($dates as $i => $date){
        echo $i." => ".get_age($date)."\n";
}
?>
And got exactly the same results :D
[WinXP Professional, Apache/2.0.49 (Win32), PHP/5.1.4]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I like AKAPJ's. I think relying on a timestamp that, for the time being, is limited to essentially last century through the first 40 years of this century is kinda wonky. There will be some big trouble when it comes to the actual format of the date passed to the function (as in, we Americans do it one way and the rest of the world does it a bit different), but I rather like the tought of subtracting the date in parts to arrive at an age.
olrac
Forum Newbie
Posts: 2
Joined: Wed May 23, 2007 1:13 am

Post by olrac »

Hello,

I have a question for this code is this the right bdate code?
because i tried these code and isn't working i mean it does but my age is 22. on that code it show my age is 23.

$dat=explode("-",$rs->fields[bdate]);
$age=(date("Y")- $dat[0]);

Thank you.
Post Reply