Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date Output to Age Output from YYYY-MM-DD

    I have figured out how to display dates entered into my database as a users birthday but I cannot for the life me figure out how to have them translate into ages.

    The current output for example on one is: 1962-07-20

    This is the script I have for the template which displays the bdate in YYYY-MM-DD

    Code:
    {section name=i loop=$answers}
    
    {$answers[i].bdate}
    
    {/section}
    and this is the PHP script that is calling sql for the bdate.

    PHP Code:
    $sql="SELECT * from signup where account_status='Active' order by UID desc limit $startfrom, $config[items_per_page]";
    $rs=$conn->execute($sql);
    $users $rs->getrows();
    STemplate::assign('answers',$users); 
    Does anyone know how I can translate the date output to display an age instead of date of birth.

    Thank you kindly.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,044
    Thanks
    2
    Thanked 316 Times in 308 Posts
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Sep 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Am I doing this right?

    PHP Code:
    $sql="SELECT bdate, CURDATE(), (YEAR(CURDATE())-YEAR(bdate)) (RIGHT(CURDATE(),5)<RIGHT(bdate,5)) AS age  FROM signup ORDER BY age;"
    $conn->execute($sql);
    STemplate::assign('profileage',$age); 
    This returns the following error...
    syntax error, unexpected T_VARIABLE
    I am sorry this is not my area, thanks for your help.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You forgot the ; at the end of the $sql line.

  • #5
    New Coder
    Join Date
    Sep 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I fixed the missing; but still the age and nothing shows now. Please help as I have been sitting here all day just to get the age to show lol.


    I have this in the PHP file..

    PHP Code:
    MORE PHP...

    elseif(
    $category=="rf")$sql="SELECT * from signup where account_status='Active' and gender like 'Couple' order by UID desc limit $startfrom, $config[items_per_page]";

    elseif(
    $category=="rd")$sql="SELECT * from video WHERE channel like '%|$_REQUEST[chid]|%' AND type='public' $active order by rand() limit $startfrom, $config[items_per_page]";



    $sql="SELECT bdate, CURDATE(), (YEAR(CURDATE())-YEAR(bdate)) (RIGHT(CURDATE(),5)<RIGHT(bdate,5)) AS age  FROM signup ORDER BY age" ;


    $rs=$conn->execute($sql);

    $users $rs->getrows();

    $start_num=$startfrom+1;

    $end_num=$startfrom+$rs->recordcount();

    $page_link="";

    $k=1;


    MORE PHP... 
    and this in the html template.

    Code:
    {section name=i loop=$answers}.... More HTML
    
    <dd>{$answers[i].bdate}</dd>
    
    <dd>Location: <strong>{if $answers[i].city ne ""}{$answers[i].city},{/if} {if $answers[i].town ne ""}{$answers[i].town}{/if} </strong></dd>
    
    <dd>Country: <strong>{$answers[i].country}</strong></dd>
    </dl>
    
    MORE HTML....
    {/section}

    Without the line
    PHP Code:
    $sql="SELECT bdate, CURDATE(), (YEAR(CURDATE())-YEAR(bdate)) (RIGHT(CURDATE(),5)<RIGHT(bdate,5)) AS age  FROM signup ORDER BY age" 
    The page loads and
    Code:
    $answers[i].bdate
    on the template shows the birthday in YYYY-MM-DD format but I want it to show the age.

  • #6
    New Coder
    Join Date
    Sep 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I hope I haven't been too confusing. Please help.

    Thanks a million.

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Is your bdate column DATE type or is it CHAR or VARCHAR type?

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    and nothing shows now
    Is error_reporting turned on?

  • #9
    New Coder
    Join Date
    Sep 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The type is a DATE Type.

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    OK good... I took a look at your query and it doesn't make sense to me. If it's valid SQL then it's news to me (and I certainly could be missing something).

    Here's how I arrive at age:
    Code:
    SELECT
    year(
        concat(
            if (
                concat(year(current_timestamp), '-', date_format(birth_dt, '%m-%d')) >= current_date,
                year(current_timestamp),
                year(current_timestamp + interval 1 year)
            ),
            '-',
            date_format(current_date, '%m-%d')))
     - year(birth_dt) as age_nr

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    YEAR(CURDATE())-YEAR(bdate)
    This doesn't work? Technically that should give this for birthday of 01-01-1980

    Code:
    2007 - 1980 = 20
    This could be wrong because of the date component (for example if your birthday is 12-01-1980 and it is now 11-01-2007), but you could do this:

    Code:
    YEAR(CURDATE() - bdate)
    That might work.

  • #12
    New Coder
    Join Date
    Sep 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got that query from the link in this post http://dev.mysql.com/doc/refman/5.0/...culations.html

    I am calling the sql from a table called signup that has the user info when they signup. The {section name=i loop=$answers} loops the answers at 20 users a page. If I have on the template $answers[i].city , $answers[i].bdate it will loop 20 users cities and birthdays in the section of html I have those answers in. My usual sql query that gets this table info is...

    Code:
    $sql="SELECT * from signup where account_status='Active' order by UID desc limit $startfrom, $config[items_per_page]";
    This queries the right table. Then I have the template assigned for this sql query for the template.

    Code:
    $rs = $conn->Execute($sql);
    $users = $rs->getrows();
    STemplate::assign('answers',$users);
    This works beautifully for all of the section looped answers. It feeds the output page the exact answers in the table...ie; YYYY-MM-DD for bdate and inputted city info.

  • #13
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Your link is bad...

    I'm not sure why you went into all that process detail-- it's not relevant.

    Here's the part of your query that doesn't make sense:
    Code:
    SELECT bdate
    , CURDATE()
    , (YEAR(CURDATE())-YEAR(bdate)) (RIGHT(CURDATE(),5)<RIGHT(bdate,5)) AS age
    FROM signup
    ORDER BY age
    It just doesn't look valid to me. The first part is fine, year - year, but then you plop a comparison in there without an "IF" or "CASE". Forgive me if it's syntax I'm not familiar with.

    Aedrin the 2007 - 1980 only works if the person has already had a birthday this year, otherwise it's a year off. So on January 1st most everyone's age will be wrong. YEAR (curdate() - bdate) has the same issue.

  • #14
    New Coder
    Join Date
    Sep 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I went into all that process detail because I dont have the slightest idea what I am doing and I wanted to make sure that I said everything that might needed to be said. I am sorry if it was unnecessary.

    So in order for it to process the bdate to age would I use your query and assign it a new template or use the same answers template.

    Code:
    SELECT
    year(
        concat(
            if (
                concat(year(current_timestamp), '-', date_format(birth_dt, '%m-%d')) >= current_date,
                year(current_timestamp),
                year(current_timestamp + interval 1 year)
            ),
            '-',
            date_format(current_date, '%m-%d')))
     - year(birth_dt) as age_nr
    I am sorry I am not an expert at this.

  • #15
    New Coder
    Join Date
    Sep 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is a query that outputs the age but it doesn't work if I try to loop the answers. This query translates the bdate in YYYY-MM-DD to age but only for one user on one page.

    PHP Code:
    $sql="select * from signup WHERE UID='$_REQUEST[UID]'";
    $rs=$conn->execute($sql);
    if(
    $rs->recordcount()>0)
    {
            
    //INCREASE VIEW COUNT
            
    $sql="update signup set profile_viewed=profile_viewed+1 WHERE UID=$_REQUEST[UID]";
            
    $conn->execute($sql);
            
    //END
            
    $dat=explode("-",$rs->fields[bdate]);
            
    $age=(date("Y")-$dat[0]);
            
    STemplate::assign('profileage',$age);
            
    $users $rs->getrows();
            
    $sql="select * from video WHERE UID=$_REQUEST[UID] $active order by VID desc limit 1";
            
    $rs=$conn->execute($sql);
            
    $vdo $rs->getrows(); 
    Code:
    {$profileage}
    I try to use this to loop the answer on another page and it loops 2007 as the answer.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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