Age field?

Discussion in 'aMember Pro v.4' started by frtyu, Aug 26, 2013.

  1. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Hi,

    i wonder if its possible to have a field that automatically calculates age based on the birthday of the member and how to do it.

    Thank you.
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    Yes, you can do it.
    I assume you want to add this field to user form for admin reference.

    Here is step by step guide:
    1. Create new additional field at aMember CP -> Configuration -> Add Fields set Field Type to SQL, SQL field type to String, Field Name to birthday and Display Type to Date
    2. Edit site.php (http://www.amember.com/docs/Site.php_file) file and add the following code
    PHP:
    //bind our function addAgeField to gridUserInitForm event
    Am_Di::getInstance()->hook->add('gridUserInitForm''addAgeField');
     
    function 
    addAgeField(Am_Event $event)
    {
        
    //get grid object from event and retrieve user record and form from it
        
    $form $event->getGrid()->getForm();
        
    $user $event->getGrid()->getRecord();
     
        
    //in case user already choose something for birthday
        
    if ($user->birthday) {
            
    //create two DateTime objects (now and birthday)
            
    $birthday Am_Di::getInstance()->dateTime->setTimestamp(amstrtotime($user->birthday));
            
    $now Am_Di::getInstance()->dateTime;
     
            
    //find the difference
            
    $diff $now->diff($birthday);
            
    $age $diff->format('%y');
     
            
    //retrieve comment element from form
            
    list($el) = $form->getElementsByName('comment');
     
            
    //create static element (just html output) and set necessary info to it
            
    $static HTML_QuickForm2_Factory::createElement('static');
            
    $static->setLabel(___('Age'))
                ->
    setContent('<div>' ___('%d years'$age) . '</div>');
     
            
    //insert our static element to user form just before comment element
            
    $el->getContainer()->insertBefore($static$el);
        }
    }
    That is all. You should see calculated age in user form in case of user already choose birthday date.
  3. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Hi,

    Thank you for your answer. I did as you suggested but im getting this error when creating a new user:

    Fatal error: Call to undefined method DateTime::setTimestamp() in /home1/mysite/public_html/amember/application/configs/site.php on line 28

    also, i cant enter a date year any earlier than 2002.

    Thank you.
  4. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    It seems you use old version of PHP that does not support DateTime::setTimestamp() yet.

    You can replace code
    PHP:
    $birthday Am_Di::getInstance()->dateTime->setTimestamp(amstrtotime($user->birthday));
    with
    PHP:
    $date getdate(amstrtotime($user->birthday));
    $birthday Am_Di::getInstance()->dateTime->setDate($date['year'], $date['mon'], $date['mday']);
    Regarding years in datepicker:
    Did you tried to change it in admin interface?

    Please add field birthday to profile form
    aMember CP -> Configuration -> Forms Editor

    and then login as user and try to set date.
  5. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Hi,

    you are right. My php ini file was configured to work as 5.2. I set it to 5.4 and it works fine now. The date picker also works now but only on the profile page. Why doesnt it work on the admin page too? It would useful.

    thank you.
  6. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    You are welcome!

    Actually it works but do not include so wide range of years. We will extend it in next release.
  7. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Hi,

    is there any way to have a quick fix for this before the next release? Its kinda urgent to me.

    Thank you.
  8. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    Yes, of course. You can do it.
    1. edit file amember/application/default/views/public/js/admin.js
    2. find initialization of datepicker and right after line of code changeYear: true, add the following line yearRange: 'c-90:c+10',
    3. save file and refresh page with admin interface in your browser.
    Actually you can find changeYear: true, only in one place in this file so just add suggested code right after it.
  9. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Hi,

    thank you for your help. However now i just found calculation errors with years before 1970. For example, if i pick September 3rd 1969 the age displayed is 56 years old which is wrong. It should say 43 instead. Can be fixed?

    Thank you again.
  10. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Yes, there is such issue. Unfortunately date below 1970 is qualified as in 2000. So your 69 is actually 2069.

    To fix this issue you need to use locale with 4 digits in year format. For English language you can use British English locale. You can change default locale at aMember CP -> Configuration -> Setup/Configuration -> Languages (Default Locale)
  11. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Hi,

    is it possible to do the same with Japanese as well? Im translating my own japanese package based on the English one.

    Thank you.
  12. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Hello?? Any answer please??
  13. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Nobody can help me on this?
  14. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    I am sorry I do not familiar with japanese locales. You can tried all available japanese locales. One of them can have year format with 4 digits.
  15. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Hi,

    how can i add a local so its available in the list to choose from?
  16. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    I think it will be better to contact us in helpdesk with this issue I hope we will find some solution for you.
  17. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    I found one workaround for you. You can edit file amember/application/default/views/public/js/admin.js
    find initialization of datepicker and right after line of code changeYear: true, add the following line shortYearCutoff: 30,

    You can tune this value (30). Please read more about this param here http://api.jqueryui.com/datepicker/#option-shortYearCutoff

    I hope it will solve your issue.
  18. frtyu

    frtyu New Member

    Joined:
    Dec 17, 2008
    Messages:
    21
    Thank you. Actually what i did is to change the getDateFormat() function in library/Am/Locale.php so it always get the 4 digit year format regardless of the locale used. Now no matter what language i use, it will always allow me to choose a 4 digit year.

    Thank you for your help.
  19. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Thank you for your update. I hope this info can be useful for someone else as well.

Share This Page