I need a genius for this one!

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

    I need a genius for this one!

    What I am trying to do, is...

    Find users in a database between two ages.

    $minage
    $maxage

    The birthdays of the users are in the database in this format
    01-30-1980

    Now this function takes a birthday in this format 1980-01-30 and
    returns the age

    function birthday ($birthday)
    {
    list($year,$mon th,$day) = explode("-",$birthday );
    $year_diff = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff== 0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
    }

    But what I need to do, is somehow only show the results from the
    database where the birthday is in between two different ages.

    How do I do this. My brain hurts enough already from writing the
    function above. Can anyone help?

  • Surfer!

    #2
    Re: I need a genius for this one!

    In message <1158561685.666 825.252900@h48g 2000cwc.googleg roups.com>,
    ameshkin <amir.meshkin@g mail.comwrites
    >What I am trying to do, is...
    >
    >Find users in a database between two ages.
    >
    >$minage
    >$maxage
    >
    >The birthdays of the users are in the database in this format
    >01-30-1980
    >
    >Now this function takes a birthday in this format 1980-01-30 and
    >returns the age
    >
    function birthday ($birthday)
    {
    list($year,$mon th,$day) = explode("-",$birthday );
    $year_diff = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff== 0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
    }
    >
    >But what I need to do, is somehow only show the results from the
    >database where the birthday is in between two different ages.
    >
    >How do I do this. My brain hurts enough already from writing the
    >function above. Can anyone help?
    >
    Do it in the SQL you use to select from the database. Of course this
    assumes you have used a date-type column to hold the date of birth!

    For MySQL see:


    Check the YEAR function.



    --
    Surfer!
    Email to: ramwater at uk2 dot net

    Comment

    • Mitul

      #3
      Re: I need a genius for this one!

      ameshkin wrote:
      What I am trying to do, is...
      >
      Find users in a database between two ages.
      >
      $minage
      $maxage
      >
      The birthdays of the users are in the database in this format
      01-30-1980
      >
      Now this function takes a birthday in this format 1980-01-30 and
      returns the age
      >
      function birthday ($birthday)
      {
      list($year,$mon th,$day) = explode("-",$birthday );
      $year_diff = date("Y") - $year;
      $month_diff = date("m") - $month;
      $day_diff = date("d") - $day;
      if ($month_diff < 0) $year_diff--;
      elseif (($month_diff== 0) && ($day_diff < 0)) $year_diff--;
      return $year_diff;
      }
      >
      But what I need to do, is somehow only show the results from the
      database where the birthday is in between two different ages.
      >
      How do I do this. My brain hurts enough already from writing the
      function above. Can anyone help?
      Hi ameshkin.

      Here is the approx solution to ur answer. You can do things by Query.
      Here is the query for that You needs to just modify little by PHP and
      Pass it on to the My-SQL u will get poper age.

      SELECT name, birth, CURRENT_DATE,
      -(YEAR(CURRENT_D ATE)-YEAR(birth))
      -- (RIGHT(CURRENT_ DATE,5)<RIGHT(b irth,5))
      -AS age
      -FROM pet;


      /***** There is nothing impossible coz Impossible itself means I M
      Possible. :)
      Regards,
      Mitul Patel

      Comment

      • Oli Filth

        #4
        Re: I need a genius for this one!

        ameshkin wrote:
        What I am trying to do, is...
        >
        Find users in a database between two ages.
        >
        $minage
        $maxage
        >
        The birthdays of the users are in the database in this format
        01-30-1980
        >
        Now this function takes a birthday in this format 1980-01-30 and
        returns the age
        >
        function birthday ($birthday)
        {
        list($year,$mon th,$day) = explode("-",$birthday );
        $year_diff = date("Y") - $year;
        $month_diff = date("m") - $month;
        $day_diff = date("d") - $day;
        if ($month_diff < 0) $year_diff--;
        elseif (($month_diff== 0) && ($day_diff < 0)) $year_diff--;
        return $year_diff;
        }
        >
        But what I need to do, is somehow only show the results from the
        database where the birthday is in between two different ages.
        I assume from the above that you are storing birthdays in the database
        as strings. This is more trouble than it's worth. Most databases have
        a date type, and functions which allow easy comparisons, etc.

        MySQL, for instance, has loads:



        --
        Oli

        Comment

        • ameshkin

          #5
          Re: I need a genius for this one!

          Yes, thats one problem. For other reasons, teh date is in the database
          as a string. When I get home, i will changbe the structure around and
          see if i can just use a simple sql query to get my results.

          Thanks for the help


          Oli Filth wrote:
          ameshkin wrote:
          What I am trying to do, is...

          Find users in a database between two ages.

          $minage
          $maxage

          The birthdays of the users are in the database in this format
          01-30-1980

          Now this function takes a birthday in this format 1980-01-30 and
          returns the age

          function birthday ($birthday)
          {
          list($year,$mon th,$day) = explode("-",$birthday );
          $year_diff = date("Y") - $year;
          $month_diff = date("m") - $month;
          $day_diff = date("d") - $day;
          if ($month_diff < 0) $year_diff--;
          elseif (($month_diff== 0) && ($day_diff < 0)) $year_diff--;
          return $year_diff;
          }

          But what I need to do, is somehow only show the results from the
          database where the birthday is in between two different ages.
          >
          I assume from the above that you are storing birthdays in the database
          as strings. This is more trouble than it's worth. Most databases have
          a date type, and functions which allow easy comparisons, etc.
          >
          MySQL, for instance, has loads:

          >
          >
          --
          Oli

          Comment

          • IchBin

            #6
            Re: I need a genius for this one!

            ameshkin wrote:
            What I am trying to do, is...
            >
            Find users in a database between two ages.
            >
            $minage
            $maxage
            >
            The birthdays of the users are in the database in this format
            01-30-1980
            >
            Now this function takes a birthday in this format 1980-01-30 and
            returns the age
            >
            function birthday ($birthday)
            {
            list($year,$mon th,$day) = explode("-",$birthday );
            $year_diff = date("Y") - $year;
            $month_diff = date("m") - $month;
            $day_diff = date("d") - $day;
            if ($month_diff < 0) $year_diff--;
            elseif (($month_diff== 0) && ($day_diff < 0)) $year_diff--;
            return $year_diff;
            }
            >
            But what I need to do, is somehow only show the results from the
            database where the birthday is in between two different ages.
            >
            How do I do this. My brain hurts enough already from writing the
            function above. Can anyone help?
            >
            Friend!, if you have to cross post make sure you have all of the
            newsgroups in to TO: statement before you send it. It stops you from
            wasting other peoples time not knowing someone else had resolved your
            problem already in another group!

            First off, you should keep the date in the database the default format
            'yyyy-MM-DD'.

            I am not handling leap years but gives you a start. You may want to
            build a procedure to do all of this and include leap year:

            $minAge = 24;
            $maxAge = 54;

            SELECT
            LASTNAME,
            FIRSTNAME,
            ROUND(DATEDIFF( CURDATE(),birth day)/365) AS 'numberOfYears' ,
            birthday
            FROM table
            WHERE
            ROUND(DATEDIFF( CURDATE(),birth day)/365) <= $maxAge AND
            ROUND(DATEDIFF( CURDATE(),birth day)/365) >= $minAge;



            --
            Thanks in Advance...
            IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
            _______________ _______________ _______________ _______________ ______________

            'If there is one, Knowledge is the "Fountain of Youth"'
            -William E. Taylor, Regular Guy (1952-)

            Comment

            • IchBin

              #7
              Re: I need a genius for this one!

              ameshkin wrote:
              What I am trying to do, is...
              >
              Find users in a database between two ages.
              >
              $minage
              $maxage
              >
              The birthdays of the users are in the database in this format
              01-30-1980
              >
              Now this function takes a birthday in this format 1980-01-30 and
              returns the age
              >
              function birthday ($birthday)
              {
              list($year,$mon th,$day) = explode("-",$birthday );
              $year_diff = date("Y") - $year;
              $month_diff = date("m") - $month;
              $day_diff = date("d") - $day;
              if ($month_diff < 0) $year_diff--;
              elseif (($month_diff== 0) && ($day_diff < 0)) $year_diff--;
              return $year_diff;
              }
              >
              But what I need to do, is somehow only show the results from the
              database where the birthday is in between two different ages.
              >
              How do I do this. My brain hurts enough already from writing the
              function above. Can anyone help?
              >
              Friend!, if you have to cross post make sure you have all of the
              newsgroups in to TO: statement before you send it. It stops you from
              wasting other peoples time not knowing someone else had resolved your
              problem already in another group!

              First off, you should keep the date in the database the default format
              'yyyy-MM-DD' as a DATE Type..

              I am not handling leap years but gives you a start. You may want to
              build a procedure to do all of this and include leap year:

              $minAge = 24;
              $maxAge = 54;

              SELECT
              LASTNAME,
              FIRSTNAME,
              ROUND(DATEDIFF( CURDATE(),birth day)/365) AS 'numberOfYears' ,
              birthday
              FROM table
              WHERE
              ROUND(DATEDIFF( CURDATE(),birth day)/365) <= $maxAge AND
              ROUND(DATEDIFF( CURDATE(),birth day)/365) >= $minAge;




              --
              Thanks in Advance...
              IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
              _______________ _______________ _______________ _______________ ______________

              'If there is one, Knowledge is the "Fountain of Youth"'
              -William E. Taylor, Regular Guy (1952-)

              Comment

              Working...