Web Analytics Made Easy -
StatCounter Correct Age - CodingForum

Announcement

Collapse
No announcement yet.

Correct Age

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Correct Age

    at the moment I'm using this function to calculate a date of birth but on some members it is correct, other times it is a year out, just wondering if anyone has any suggestions? thanks

    PHP Code:
    function dateDiff($dformat$endDate$beginDate)
    {
    $date_parts1=explode($dformat$beginDate);
    $date_parts2=explode($dformat$endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return 
    $end_date $start_date;
    }

    $dob $birthmonth.'/'.$birthdate.'/'.$birthyear;
    $age round(dateDiff("/"date("m/d/Y"time()), $dob)/3650); 
    Matt Davenport - Freelance Web Developer

  • #2
    This MakeTime() function is a function I use, which I stole-- er, borrowed, from someone else (I think a user post at www.php.net). This code figures out someone's age based on their birthday:

    PHP Code:
    //assumes a birthday date format of YYYY-MM-DD
    list ($bYear$bMonth$bDay) = explode ('-'$wkBday);
    $wkBdayText date('F jS, Y'MakeTime(1100$bMonth$bDay$bYear));
    $agetime MakeTime() - MakeTime(1100$bMonth$bDay$bYear);
    $wkAge = (strftime("%Y",(MakeTime() - MakeTime(1100$bMonth$bDay$bYear)))) - (strftime("%Y",0)) - 1;

    function 
    MakeTime() {
       
    $objArgs func_get_args();
       
    $nCount count($objArgs);
       if (
    $nCount 7)
       {
           
    $objDate getdate();
           if (
    $nCount 1)
               
    $objArgs[] = $objDate["hours"];
           if (
    $nCount 2)
               
    $objArgs[] = $objDate["minutes"];
           if (
    $nCount 3)
               
    $objArgs[] = $objDate["seconds"];
           if (
    $nCount 4)
               
    $objArgs[] = $objDate["mon"];
           if (
    $nCount 5)
               
    $objArgs[] = $objDate["mday"];
           if (
    $nCount 6)
               
    $objArgs[] = $objDate["year"];
           if (
    $nCount 7)
               
    $objArgs[] = -1;
       }
       
    $nYear $objArgs[5];
       
    $nOffset 0;
       if (
    $nYear 1970)
       {
           if (
    $nYear 1902)
               return 
    0;
           else if (
    $nYear 1952)
           {
               
    $nOffset = -2650838400;
               
    $objArgs[5] += 84;
               
    // Apparently dates before 1942 were never DST
               
    if ($nYear 1942)
                   
    $objArgs[6] = 0;
           }
           else
           {
               
    $nOffset = -883612800;
               
    $objArgs[5] += 28;
           }
       }

       return 
    call_user_func_array("mktime"$objArgs) + $nOffset;

    Comment


    • #3
      Originally posted by Fumigator View Post
      This MakeTime() function is a function I use, which I stole-- er, borrowed, from someone else (I think a user post at www.php.net). This code figures out someone's age based on their birthday:

      PHP Code:
      //assumes a birthday date format of YYYY-MM-DD
      list ($bYear$bMonth$bDay) = explode ('-'$wkBday);
      $wkBdayText date('F jS, Y'MakeTime(1100$bMonth$bDay$bYear));
      $agetime MakeTime() - MakeTime(1100$bMonth$bDay$bYear);
      $wkAge = (strftime("%Y",(MakeTime() - MakeTime(1100$bMonth$bDay$bYear)))) - (strftime("%Y",0)) - 1;

      function 
      MakeTime() {
         
      $objArgs func_get_args();
         
      $nCount count($objArgs);
         if (
      $nCount 7)
         {
             
      $objDate getdate();
             if (
      $nCount 1)
                 
      $objArgs[] = $objDate["hours"];
             if (
      $nCount 2)
                 
      $objArgs[] = $objDate["minutes"];
             if (
      $nCount 3)
                 
      $objArgs[] = $objDate["seconds"];
             if (
      $nCount 4)
                 
      $objArgs[] = $objDate["mon"];
             if (
      $nCount 5)
                 
      $objArgs[] = $objDate["mday"];
             if (
      $nCount 6)
                 
      $objArgs[] = $objDate["year"];
             if (
      $nCount 7)
                 
      $objArgs[] = -1;
         }
         
      $nYear $objArgs[5];
         
      $nOffset 0;
         if (
      $nYear 1970)
         {
             if (
      $nYear 1902)
                 return 
      0;
             else if (
      $nYear 1952)
             {
                 
      $nOffset = -2650838400;
                 
      $objArgs[5] += 84;
                 
      // Apparently dates before 1942 were never DST
                 
      if ($nYear 1942)
                     
      $objArgs[6] = 0;
             }
             else
             {
                 
      $nOffset = -883612800;
                 
      $objArgs[5] += 28;
             }
         }

         return 
      call_user_func_array("mktime"$objArgs) + $nOffset;


      Hey, thanks for your fast reply!

      I'm not sure which data needs to be input into the actual function though, this bit is slightly blurry to me:


      PHP Code:
      list ($bYear$bMonth$bDay) = explode ('-'$wkBday);
      $wkBdayText date('F jS, Y'MakeTime(1100$bMonth$bDay$bYear));
      $agetime MakeTime() - MakeTime(1100$bMonth$bDay$bYear);
      $wkAge = (strftime("%Y",(MakeTime() - MakeTime(1100$bMonth$bDay$bYear)))) - (strftime("%Y",0)) - 1
      Matt Davenport - Freelance Web Developer

      Comment


      • #4
        $wkBday is the only input and needs to be in YYYY-MM-DD format. Or you can mess around with those 4 lines of code to fit it to your needs.

        The relevant thing is the MakeTime() function overcomes the silly date range limitations that unfortunately come with PHP. The PHP function mktime() could be used, but it won't work if the birthday is older than 1969-12-31. So, essentially, MakeTime() is a replacement for mktime(), and uses the same inputs.

        Comment


        • #5
          Ahh, I see now, that seems to have worked fine, thanks alot
          Matt Davenport - Freelance Web Developer

          Comment

          Working...
          X