How do you add variables and numbers together

Discussion in 'PHP' started by Imozeb, Feb 2, 2010.

  1. #1
    I am trying to create an age function where the user enters the year they where born into a database through a form and later when they return to their profile page it tells them how old they are by subtracting todays date from the date they were born which will be their current age.

    How do I subtract my server variable to my time variables? (in red) it just keeps printing it out on the screen or giving me an error.

    My code looks like this

    <?php
    $loginusername = testname;
    //Get date
    $date=getdate(date("U"));

    //query
    mysql_select_db($database_username, $username);
    $query_profile = "SELECT * FROM UserNames WHERE username = '$loginusername'";
    $profile = mysql_query($query_profile, $username) or die(mysql_error());
    $row_profile = mysql_fetch_assoc($profile);
    $totalRows_profile = mysql_num_rows($profile);


    //get age
    ?>

    <html>
    <body>
    <?PHP echo $age = $row_profile['year'] - $date[year]; ?>
    </body>
    </html>


    Thanks in advance.

    ~Imozeb
     
    Last edited: Feb 2, 2010
    Imozeb, Feb 2, 2010 IP
  2. Nei

    Nei Well-Known Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    170
    #2
    Nobody will tell you all basics. You should read some books or manuals.
     
    Nei, Feb 2, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I know but could you just help me with this one script, how do I subtract my server variables from my time variables?
     
    Imozeb, Feb 2, 2010 IP
  4. Nei

    Nei Well-Known Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    170
    #4
    I don't know the structure of your database.
    Maybe this example will help you:

    <?php
    //Get date
    $date=date('Y');
    $row_profile['year']=2005;
    ?>
    
    <html>
    <body>
    <?PHP echo $row_profile['year'] - $date;?>
    </body>
    </html>
    PHP:
     
    Nei, Feb 2, 2010 IP