PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Dec 12, 2017 9:17 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 44 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject:
PostPosted: Wed Jun 21, 2006 7:41 am 
Offline
Forum Contributor
User avatar

Joined: Sun May 07, 2006 5:19 am
Posts: 101
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.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 7:56 am 
Offline
DevNet Resident
User avatar

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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.

Quote:
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 9:21 am 
Offline
Forum Contributor
User avatar

Joined: Sun May 07, 2006 5:19 am
Posts: 101
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 11:11 am 
Offline
DevNet Resident
User avatar

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 11:20 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
~Verminox is your system 32 bit or 64 bit?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 11:20 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
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:
Syntax: [ Download ] [ Hide ]
[feyd@home]>php -r "echo date('Y-m-d H:i:s', -1 * 120 * 24 * 3600);"
1969-09-02 19:00:00


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 11:51 am 
Offline
Forum Contributor
User avatar

Joined: Sun May 07, 2006 5:19 am
Posts: 101
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 12:38 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:07 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6883
Perhaps it has something to do with whether the variable contains a string or integer?

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:24 pm 
Offline
Forum Contributor
User avatar

Joined: Sun May 07, 2006 5:19 am
Posts: 101
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?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:34 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 9:53 am 
Offline
Forum Contributor
User avatar

Joined: Sun May 07, 2006 5:19 am
Posts: 101
I tried your function on my PC. Here goes:

Syntax: [ Download ] [ Hide ]
<?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:

Syntax: [ Download ] [ Hide ]
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 10:07 am 
Offline
DevNet Resident
User avatar

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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:
Syntax: [ Download ] [ Hide ]
<?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]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 12:33 pm 
Offline
Site Administrator
User avatar

Joined: Tue Sep 09, 2003 6:04 pm
Posts: 14293
Location: Fremont, CA, USA
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 1:23 am 
Offline
Forum Newbie

Joined: Wed May 23, 2007 1:13 am
Posts: 2
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 44 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group