4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 PHP: calculating age from dob using mktime()
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Sat Mar 08, 2003 10:51 am (14 years, 7 months ago) Reply with QuoteBack to Top

I use this piece of code to work out someones age from their DOB:
Code:
function calculate_age($dob_day, $dob_month, $dob_year) {
   $bdayunix = mktime ("", "", "", $dob_month, $dob_day, $dob_year);
   $nowunix = time();
   $ageunix = $nowunix - $bdayunix;
   $age = floor($ageunix / (365 * 24 * 60 * 60));
   
   //bug fix to stop negative ages being returned
   if($age < 0) {
      $age = $age + 136;
   }
   
   return $age;
}


I've just noticed that its no longer working how it used to. It won't return an age over 33. Here I have echoed out all of the variables - the problem is occuring when creating $bdayunix

Zeonhost wrote:
day: 10
month: 6
year: 1950
Birthday Unix: -1
Now Unix: 1047119433
Age Unix: 1047119434
Age1: 33
Returned Age: 33
I'm now 33 years old.


As I was convinced it used to work on my old host I tried it on 34SP servers and surprise surprise it worked how expected!! see below:

34SP wrote:
day: 10
month: 6
year: 1950
Birthday Unix: -617328000
Now Unix: 1047119516
Age Unix: 1664447516
Age1: 52
Returned Age: 52
I'm now 52 years old.


Well I guess the reason for this is here:
http://www.php.net/manual/en/function.mktime.php
not that it really makes much sense to me or helps in any way.

Any one got any ideas for an alternative way to calculate an age without using mktime()?
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Sat Mar 08, 2003 5:03 pm (14 years, 7 months ago) Reply with QuoteBack to Top

I'm not sure where the problem lies, but could you just try this script:

http://www.faqts.com/knowledge_base/view.phtml/aid/1943/fid/18

I'm wondering whether it's to do with Unix time starting in 1970 (which would be 33 years ago)...

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Sat Mar 08, 2003 5:30 pm (14 years, 7 months ago) Reply with QuoteBack to Top

Daniel wrote:
I'm wondering whether it's to do with Unix time starting in 1970 (which would be 33 years ago)...
yes any date before 1970 mktime returns -1 - its something to do with a version of red hat and a library that php uses?? Its obviously something that only effects certain installations.

Unfortunately that example still uses mktime() which is the cause of the problem Sad
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Sat Mar 08, 2003 5:36 pm (14 years, 7 months ago) Reply with QuoteBack to Top

Is this what you're talking about?

Quote:
Red Hat 7.3's glibc contains a strange change in behaviour regarding the mktime() function. It affects PHP's mktime() function, so that on Red Hat 7.3 systems, mktime() will always return -1 for dates before Jan 1st 1970 (more or less - time zones may distort this rule of thumb slightly).

If you use Red Hat Linu x7.3 and need the "old" behaviour (which is used on nearly all other unix systems), then use the glibc package set at http://rpms.arvin.dk/glibc/rh73/

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Sat Mar 08, 2003 6:55 pm (14 years, 7 months ago) Reply with QuoteBack to Top

Yes thats what I read:

from phpinfo on Zeonhost:
System Linux redhat73vm.psoft 2.4.18-3 #1 Thu Apr 18 07:32:41 EDT 2002 i686 unknown

I guess I could ask if Zeonhost are willing to use the alternative glibc package...
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




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 vote in polls in this forum.


Page generation time: 0.051147 seconds :: 17 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme