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.
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: 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 Edit site.php (http://www.amember.com/docs/Site.php_file) file and add the following code PHP: //bind our function addAgeField to gridUserInitForm eventAm_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.
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.
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.
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.
You are welcome! Actually it works but do not include so wide range of years. We will extend it in next release.
Hi, is there any way to have a quick fix for this before the next release? Its kinda urgent to me. Thank you.
Hello, Yes, of course. You can do it. 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 yearRange: 'c-90:c+10', 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.
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.
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)
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.
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.
I think it will be better to contact us in helpdesk with this issue I hope we will find some solution for you.
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.
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.