Results 1 to 7 of 7

Thread: Calculating the age of a person...

  1. #1

    Thread Starter
    Addicted Member dmr's Avatar
    Join Date
    Jul 2000
    Location
    West-Germany :) Timezone: GMT +1 [DST] North.......: 52° 16’ 09” East...: 10° 31’ 16”
    Posts
    255

    Unhappy Calculating the age of a person...

    Hi all!

    I have a table (mySQL) which is filled with member-information. One field in this table is the birthdate of a person in yyyy-mm-dd format. I converted this date to a unix-timestamp and calculated the difference to the timestamp of today.

    But there's a problem...
    A member who had been born before the 1st of January in 1970 has no age...

    Infact PHP doesn't allow a timestamp to be negative... So what do I have to do? Should I forbid persons born before 1970 to become members??

    The algorithm should be programmed in PHP (4).

    Thanks in advance,
    dmr.
    Code that I author is neither elegant nor efficient. It is, however, functional. Once I get something that works, I'm generally satisfied with myself - I mean, if it works, that's good enough, right?

    Originally posted by aknisely
    Sorry, but I feel uncomfortable on CC with clothes on
    __________________
    The truth ... is out there!

  2. #2
    I'm guessing that's because mySQL uses epochs to store dates, which are the number of milliseconds since January 1, 1970 (not sure about the January part but sure about the 1970 part). Maybe you can offset everybody's birthday by 100 years? i.e., if I was born in 1984, it would be stored as 2084.

  3. #3

    Thread Starter
    Addicted Member dmr's Avatar
    Join Date
    Jul 2000
    Location
    West-Germany :) Timezone: GMT +1 [DST] North.......: 52° 16’ 09” East...: 10° 31’ 16”
    Posts
    255
    Thanks for your reply, filburt1, but in 2038, 19th January at 03:14.07 GMT +1 we're going to get another problem...

    The maximum timestamp PHP allows for GMT +1 is 2147480047
    (2^31 - 1 [- OffSet]).

    Perhaps you get it wrong with mySQL. The database is not the problem but the algorithm to get the age.

    Another idea?
    Thanks in advance,
    dmr.
    Code that I author is neither elegant nor efficient. It is, however, functional. Once I get something that works, I'm generally satisfied with myself - I mean, if it works, that's good enough, right?

    Originally posted by aknisely
    Sorry, but I feel uncomfortable on CC with clothes on
    __________________
    The truth ... is out there!

  4. #4
    Sorry, that's all I know.

  5. #5
    scoutt
    Guest
    what if you did the calculation before you put it in the time stamp.
    so do all the calculations then turn it and the date to a timestamp?

  6. #6
    You could store the day, month, and year in separate variables (ints or whatever it is for PHP/mySQL).

  7. #7

    Thread Starter
    Addicted Member dmr's Avatar
    Join Date
    Jul 2000
    Location
    West-Germany :) Timezone: GMT +1 [DST] North.......: 52° 16’ 09” East...: 10° 31’ 16”
    Posts
    255
    Okay. It's neither elegant nor efficient... But it works.

    Though I am interested in doing it with negative timestamps. Any ideas?

    Again: The problem is not storing the variable!


    This is what works fine for me if somebody is interested:

    PHP Code:
    <?php

       
    function GetAge($date1$date2) {
          list (
    $y1$m1$d1) = split('-'$date1);
          list (
    $y2$m2$d2) = split('-'$date2);

          if ( (
    $m2 $m1) or ($m2 == $m1 and $d2 $d1) ) {
             return (
    $y2-$y1-1);

          }

          else {
             return (
    $y2-$y1);

          }
       }

       
    $date1 "1969-12-31";
       
    $date2 "2001-09-23";

       print 
    GetAge($date1$date2);

    ?>
    Thanks to you!
    dmr.
    Code that I author is neither elegant nor efficient. It is, however, functional. Once I get something that works, I'm generally satisfied with myself - I mean, if it works, that's good enough, right?

    Originally posted by aknisely
    Sorry, but I feel uncomfortable on CC with clothes on
    __________________
    The truth ... is out there!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width