Perfect age calculator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcel

    Perfect age calculator

    Hello all,

    I am looking for a perfect PHP age calculator which takes someones date of
    birth as an argument and returns the person's age.

    I went over several tutorials, help and newsgroups an found lots of
    calculators, tried many times to make the perfect script but did not
    find/make the perfect one.

    Simple test:

    if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 07-07-2005 then i
    should be 1 year old

    if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 06-07-2005 then i
    should be 0 years old

    Lots of scripts i've found did not pass this test succesfull, so if someone
    can point me to the 'golden' script then i would be very thankfull!

    Regards,

    Marcel


  • Markus L.

    #2
    Re: Perfect age calculator

    Am Thu, 07 Jul 2005 22:59:56 +0200 schrieb Marcel:
    [color=blue]
    > Hello all,
    >
    > I am looking for a perfect PHP age calculator which takes someones date of
    > birth as an argument and returns the person's age.
    >
    > I went over several tutorials, help and newsgroups an found lots of
    > calculators, tried many times to make the perfect script but did not
    > find/make the perfect one.
    >
    > Simple test:
    >
    > if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 07-07-2005 then i
    > should be 1 year old
    >
    > if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 06-07-2005 then i
    > should be 0 years old
    >
    > Lots of scripts i've found did not pass this test succesfull, so if someone
    > can point me to the 'golden' script then i would be very thankfull!
    >
    > Regards,
    >
    > Marcel[/color]

    The script is written in 2 minutes:
    <?php
    $birthday = '07-07-2004';
    $today = date('d-m-Y');

    $a_birthday = explode('-', $birthday);
    $a_today = explode('-', $today);

    $day_birthday = $a_birthday[0];
    $month_birthday = $a_birthday[1];
    $year_birthday = $a_birthday[2];
    $day_today = $a_today[0];
    $month_today = $a_today[1];
    $year_today = $a_today[2];

    $age = $year_today - $year_birthday;

    if (($month_today < $month_birthday ) || ($month_today == $month_birthday && $day_today < $day_birthday))
    {
    $age--;
    }
    ?>

    --
    -------------------------------------------------------
    Try this: SCA the Smart Class Archive for PHP

    -------------------------------------------------------

    Comment

    • Mladen Gogala

      #3
      Re: Perfect age calculator

      On Thu, 07 Jul 2005 22:59:56 +0200, Marcel wrote:
      [color=blue]
      > Hello all,
      >
      > I am looking for a perfect PHP age calculator which takes someones date of
      > birth as an argument and returns the person's age.[/color]

      <?php
      print "You're too old!\n";
      ?>
      --


      Comment

      • Marcel

        #4
        Re: Perfect age calculator


        "Markus L." <nobody@proje ct-sca.org> schreef in bericht
        news:pan.2005.0 7.07.21.25.10.9 49260@project-sca.org...[color=blue]
        > Am Thu, 07 Jul 2005 22:59:56 +0200 schrieb Marcel:
        >[color=green]
        >> Hello all,
        >>
        >> I am looking for a perfect PHP age calculator which takes someones date
        >> of
        >> birth as an argument and returns the person's age.
        >>
        >> I went over several tutorials, help and newsgroups an found lots of
        >> calculators, tried many times to make the perfect script but did not
        >> find/make the perfect one.
        >>
        >> Simple test:
        >>
        >> if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 07-07-2005 then i
        >> should be 1 year old
        >>
        >> if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 06-07-2005 then i
        >> should be 0 years old
        >>
        >> Lots of scripts i've found did not pass this test succesfull, so if
        >> someone
        >> can point me to the 'golden' script then i would be very thankfull!
        >>
        >> Regards,
        >>
        >> Marcel[/color]
        >
        > The script is written in 2 minutes:
        > <?php
        > $birthday = '07-07-2004';
        > $today = date('d-m-Y');
        >
        > $a_birthday = explode('-', $birthday);
        > $a_today = explode('-', $today);
        >
        > $day_birthday = $a_birthday[0];
        > $month_birthday = $a_birthday[1];
        > $year_birthday = $a_birthday[2];
        > $day_today = $a_today[0];
        > $month_today = $a_today[1];
        > $year_today = $a_today[2];
        >
        > $age = $year_today - $year_birthday;
        >
        > if (($month_today < $month_birthday ) || ($month_today == $month_birthday
        > && $day_today < $day_birthday))
        > {
        > $age--;
        > }
        > ?>
        >
        > --[/color]

        Thank you!!!


        Comment

        • imaekphp@gmail.com

          #5
          Re: Perfect age calculator

          A much more efficient method would just use unix time; the drawback,
          however, is that it only goes up to 1970.
          An example
          <?
          getAge($year,$m onth,$day) {
          $then = mktime(1,1,1,$y ear,$month,$day );
          return(floor((t ime()-$then)/31556926));
          }
          ?>

          This should return a round number (e.g. 15, 20, 25) but if you want it
          to return the decimal form of the year that has been completed, remove
          the floor() function, which, if the user was exactly 20 and 1/2, would
          return 20.5, but most likely would return their age and a long string
          of decimals (20.32432544354 5634643545...)

          Not that that really answeres your question at all, but I had just been
          thinking about this for a while this afternoon, and when I saw this
          topic (an excuse to type it up and fully reason it out,) I decided to
          jump at it. ;-)

          --imaek

          Comment

          Working...