Display age on profile page
-
Hi
Looking to get the “Date of Birth” field from the members profile and place it in two place, at the top of the profile page under the user name and on the members directory. I also need to display the age and not the date. Any ideas about how it do this?
The only thing I could track down was http://code.hyperspatial.com/507/buddypress-age/ but I couldn’t get it to work.
Any ideas?
Cheers
-
Good question i myself was going to ask this question you beat me too it. I’m assuming there is some coding that has to be done to do this.
Yeah, not had much luck, can’t find anything online, has anyone got any ideas?
I know what you mean but a few suggestions if anyone plans on making an users age plug-in.
Make it so the date of birth transfers to age and allows the user to show their age or not show, or show date of birth or not show date of birth. But just show month and date. basically make it optional. The many few things that seem small can be big. Especially if you’re making a great looking social network it should be user friendly for site owner admins and users etc. etc. I’d keep my fingers crossed on this.The top part of a member profile is in the /members/single/member-header.php template, and each user in the member directory uses the /members/members-loop.php template.
Read this page a code snippet for displaying profile fields in member header https://codex.buddypress.org/extending-buddypress/tips-tricks/
Thats great, got the calling the code into the header, but any ideas on converting the date of birth into an age?
http://www.google.com/search?q=php+convert+date+to+age&ie=UTF-8&oe=UTF-8&hl=en&client=safari
Google converting date to age
//Calculate someone's age based on a date of birth string $year = date("Y"); // Get this year as a string, date(string format, [timestamp default: now]) $dobtime = strtotime("04/09/1980"); // Get date of birth as a timestamp $age = $year - date("Y",$dobtime); // Take advantage of auto conversion to subtract this year from the year of the DOB $bday = date("m/d/",$dobtime).$year; // Create a timestamp from their month and day of birth combined with this year if((time()-strtotime($bday))<0) $age = $age - 1; // Compare their bday for this year to current timestamp and subtract one from the year if their birthday has not occurred yet
@modemlooper and @djpaul for a non coder how would i place the code? Because this is like science to me.. I’m only good at copy and paste, lol. which line would i place it under etc, thanks.
Don’t have time to work it out right now. Why dont you just have a profile field asking age?
No rush i understand well i was working with the profile search plugin and bp xtras sign up plugin.
But haven’t actually gotten them to display age. just date of birth. the profiles search plugin lets you set an age range but still not show the actual age and there was just an update. Maybe i should try contacting them to see if they can make that an option.Thanks for your help. the bp xtras signup lets you set the age allowed during registration. Bp xtra signup allows you to set the age lets say 13 the youngest etc. But hopefully there is a solution…
So if i make a dropdown age field will it show the users birth date internally?For now just supply a field title age and then user can enter the number
The only issue with having an age field that users fill in themselves is that after a year it’s out of date, and the only way to be correct is to have the user update it, but the likelihood of that happening very unlikely. Would really appreciate a resolution to this, some coding god please save the day!
@kirkslater,
Since you asked it in another thread, here is what you needPut this function in your functions.php or bp-custom.php
/** * Get Age from BuddyPress date of Birth * <a href='https://buddypress.org/community/members/param/' rel='nofollow'>@param</a> string $dob_field_name :name of the DOB field in xprofile, like Dob or Date of Birth * <a href='https://buddypress.org/community/members/param/' rel='nofollow'>@param</a> int $user_id : the user for which you want to retrieve the age * <a href='https://buddypress.org/community/members/param/' rel='nofollow'>@param</a> string $format: the way you want to print the difference, look t for the acceptable agrs * @return string :the formatted age in year/month */ function bpdev_get_age_from_dob($dob_field_name,$user_id=false,$format="%y Years, %m Month(s), %d days"){ if(!$user_id) $user_id=bp_displayed_user_id (); $dob_time=xprofile_get_field_data($dob_field_name, $user_id);//get the datetime as myswl datetime $dob=new DateTime($dob_time);//create a DateTime Object from that $current_date_time=new DateTime();//current date time object //calculate difference $diff= $current_date_time->diff($dob);//returns DateInterval object //format and return return $diff->format($format); }
and Now you can output the age anywhere using
echo "Age".bpdev_get_age_from_dob("DOB Field Name");//display for the displayed user //or echo "Your Age".bpdev_get_age_from_dob("DOB Field Name",bp_loggedin_user_id());//display for logged in user
You can pass the Dob field name, user_id and the format in which you want to show the difference.
here is the gist in case the code gets garbled: https://gist.github.com/1257476
Hope that helps.PS: It will need php 5.3.3 or above.
Your a legend! Thanks for taking the time to look at this
No problem. glad it helped
@sbrajesh can you put this into a plugin please? I’m not sure if I got your post explaining it, so it would be easier if in form of a permanent plugin. This can further be very handy integrating with some ” coming birthdays ” plugin, but still that basic age showing is much needed. thank you.
@SBrajesh I tried the codes you gave, but
I got an error when put in echo:
Fatal error: Call to undefined method DateTime::diff() in /home/dom/public_html/wp-content/plugins/buddypress/bp-themes/bp-default/functions.php on line 377Line 377 is this – $diff= $current_date_time->diff($dob);//returns DateInterval object
Any idea how to correct that?
Thanks.I created a “date selector” field named “Birthday” and then added the code brajesh posted to my functions.php and then called the function like this to my members-header.php file:
“
and it is not displaying anything. can someone help??
Don’t mind, it is working….
Hi tislam100
How have you managed to get this working on members-header.php
Thanks for your answer
hey there guys how would i be able to show the code
‘ echo “Age”.bpdev_get_age_from_dob(“DOB Field Name”);//display for the displayed user
//or
echo “Your Age”.bpdev_get_age_from_dob(“DOB Field Name”,bp_loggedin_user_id());//display for logged in user’as a profile field on the users profiles?
Sorry to bring up an old tread @sbrajesh, is there a way to show birth day and month instead of age e.g Birthday: January, 2
thanks
Same as before but:
echo $dob->format(‘F, d’);
@sgr33n, do you mean `format(‘F, d’); ?>`
please can you explain as i have no code knowledge.
thanks
- The topic ‘Display age on profile page’ is closed to new replies.