SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Adding + 13 years to a date variable.

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding + 13 years to a date variable.

    Hey guys,

    i store the users birthdate in a field in my members table. It is a date type, and i'm trying to make a section of the site which is only for 18+.

    Date is stored like this

    YYYY/MM/DD

    I grab the value from the DB and then i want to add 18 years to the current date so i can do this

    $birthdate = $birthdate + 18 years.
    $currentdate = date("Y-m-d");
    if($birthdate > $currentdate) {
    "You are not 18";
    }

    So i guess i just need to know how to add 18 years to the variable :X

    Any help? thanks

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this function to calculate age
    PHP Code:
    function getage($strdate)
    {
        
    $dob explode("/",$strdate);
        if(
    count($dob)!=3)
        {
          return 
    0;
        }
        
    $y $dob[0];
        
    $m $dob[1];
        
    $d $dob[2];
        if(
    strlen($y)!=4)
        {
          return 
    0;
        }
        if(
    strlen($m)!=2)
        {
          return 
    0;
        }
        if(
    strlen($d)!=2)
        {
          return 
    0;
        }
      
    $y += 0;
      
    $m += 0;
      
    $d += 0;
      if(
    $y==0) return 0;
      
    $rage date("Y") - $y;
      if(
    date("m")<$m)
      {
        
    $rage-=1;
        
      }else{
        if((
    date("m")==$m)&&(date("d")<$d))
        {
          
    $rage-=1;
        }
      }
      return 
    $rage;


  3. #3
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot

  4. #4
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    Shelton, CT
    Posts
    1,227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's way too complicated

    PHP Code:
    $d '1980/11/24';
    echo 
    date('y-m-d'strtotime('+18 years'strtotime($d))); 
    Might be a better idea to store whether or not the user is over 18 in the session/cookie when they login

  5. #5
    Non-Member
    Join Date
    Nov 2007
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    that's way too complicated

    PHP Code:
    $d '1980/11/24';
    echo 
    date('y-m-d'strtotime('+18 years'strtotime($d))); 
    Might be a better idea to store whether or not the user is over 18 in the session/cookie when they login

    Try his response Alot cleaner/easier.

Bookmarks

Posting Permissions

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