Buddypress Age

DescriptionBuddypress snippet by - 07/16/10

These two functions work together to calculate a buddypress users age. Keep in mind that you need to have a 'birthday' profile field for this to work.

You can call either function by passing the user Id, so omega_get_age(22); to get the age of user ID22 or omega_get_birthday(22); to get their birthday, don't forget to echo them out. You can also replace the id number with: bp_loggedin_user_id() to get the current users info.
<?php

// Get Birthday
function omega_get_birthday($user_id){
	if (bp_has_profile('user_id=' . $user_id)) :
	while (bp_profile_groups()) : bp_the_profile_group();
	while (bp_profile_fields()) : bp_the_profile_field();
	if (bp_field_has_data()) :
	$fieldname = bp_get_the_profile_field_name();
	if($fieldname == 'Birthday'){$the_birthday = strip_tags(bp_get_the_profile_field_value());}
	endif;
	endwhile;
	endwhile;
	endif;
	
	return $the_birthday;
}

function omega_get_age($member_id){
	$the_birthday = omega_get_birthday($member_id,'Birthday');
	if($the_birthday == ''){return $the_birthday;}
	$to_replace = array(' ',',');
	$replace_with =  array('-','');
	$converted_birthday = str_replace($to_replace,$replace_with,$the_birthday);
	list($month,$day,$year) = explode("-",$converted_birthday);
	$time_month = strptime($month, '%B');
	$converted_month =  $time_month['tm_mon']+1;
	$year_diff  = date("Y") - $year;
	$month_diff = date("m") - $converted_month;
	$day_diff   = date("d") - $day;
	if($day_diff < 0 && $month_diff < 0) $year_diff--;
	else if( $month_diff == 0 && $day_diff < 0) $year_diff--;
	return $year_diff;
}

?>
  • Share
Authored by: Adam J Nowak
http://hyperspatial.com

41 Responses to “Buddypress Age”

  1. Hi I get errors:
    Cannot redeclare omega_get_birthday() (previously declared in .../members-loop.php:43) in ....members-loop.php on line 43
    and how shall I echo it whats the php code for that?

  2. You probably have two copies of the omega_get_birthday() function in your code, even in a separate file. Just remove the other copy or rename the function.

    To echo the value just echo the function: echo omega_get_birthday(1);

  3. Hello again. I've installed a fresh bp site where could be that function you are talking about??? and how shall I rename it ? my php isnt that good for wordpress .kind regards

  4. I used the omega_ prefix for the functions, this is actually a theme name of mine that uses the above snippet. Using a unique prefix for a function lowers the risk of that function existing and then getting the 'unable to redeclare error'.

    If you are still getting that error that means that you pasted the omega_get_birthday() function twice in your code. I do not believe buddypress has a function by that name. But a last ditch effort is to just change the omega_ to something else everywhere in the snippet.

  5. Thanks for your very very useful code!
    I am having a problem implementing the code in the members-loop.php file. I would like to have the age displayed next to all members, but using bp_loggedin_user_id() gives every user the same age (the age of the person logged in...).
    I was able to properly use echo omega_get_age(bp_displayed_user_id()); in the member-header file to display the age on the user's profile page, but am stuck regarding the member-loop. Hopefully you have some idea?
    Lastly, all ages are displaying one year younger, and I'm not sure why. For example, someone born in May 1986 is shown as 23 years old...

    Thanks again!

  6. I had a logic error in the script, I think I fixed it and updated the post.

    Try accessing the member id from within the members loop using: $member_id = bp_get_member_user_id();

    Have fun, let me know if it returns the right age now, I did not have time to test it properly.

  7. Great! Thanks for that super-quick response. The code update and suggestion for the member loop worked perfectly! :)

  8. Hey, feel free to store your code here on the site, It is backed up often and you can sort through all your code based on your author name for future use. In my daily dev job I can't function without this site.

  9. I really want to display a users age not their birthday on their profile and don't understand how I use this code, can you please explain further

  10. First off you need to have the birthday field in the buddypress profile, you can add that from the buddypress admin pages.

    Next place these two functions in your child theme functions.php file, if you are not doing a child theme then they go in the buddypress functions.php - child themes rule so check out how to use them on buddypress.org

    Then just call the omega_get_age($member_id) function from a template file and that's it. You will need to pass the $member_id either as a number to test it out or get that programatically using a buddypress function that gets the user's id.

  11. First of all thank you very much for your codes !! I' ve already used 3 of them , unfortunately I am a noob yet . so some things I don't get like I should . I have the same problem like question nr.5 here , with the difference that he understood your answer nr.6:
    "Try accessing the member id from within the members loop using: $member_id = bp_get_member_user_id(); " ,
    Is this the only code I gotta put there or I miss something ? It calls only member ID . Can you pls. write here exact code I should put in member-loop.php to get the result right behind the name of every member ? Thanks your code above I already have it perfectly running in the single page, but not in members directory , I run latest buddypress 1.2.8 default theme and the latest wordpress 3.1 , Thank you Daniel .

  12. You need the member id to run the function, so the member id is available in the members loop because you are looping through members or a single member.

    So wherever you have access to the member id in your script, just call the function like this, and pass it the $member_id as the argument:

    $member_id = bp_get_member_user_id();
    echo omega_get_age($member_id);

    Hopefully that helps, you can get more help on codex.buddypress.org in the members loop section.

  13. Working perfectly !! thank you very much !!

  14. Hey , the age is sometimes one year older then the age is.

    like me: my birthday is : 17.07.1969 ( 17. Juli. 1969 )

    when tip in 17.7.1969 - the age is 41 - thats right

    when tip in 17.9.1969 the age is 42 !!! not right

    please help

  15. Odd, I have this running on a couple sites without issues. Strange that the day would change the year. If i come up with a fix I will post it.

  16. Hi again from Germany

    mhhh, i try some differnt things:

    1. i was born at 17. July 1969

    when i put in my age: yes, right show the right age

    NOW. Today is the 15. June -

    when i set my birthday to 14. June 1969 i am : 42 Years Old - right !

    but when i set my birthday to 20. June 1969 : I am still 42 Year OLD !!

    when i set my birthday to : 20. December 1969 : I am 41 years OLD ????

    what´s going wrong?

    Pleaae help me, pleas send me a email to : [email protected]

    thank you

  17. I updated the snippet and added this conditional: else if( $month_diff == 0 && $day_diff < 0) $year_diff--;

    Give it a try, don't forget that there may be server time differences depending on what your server or wordpress is set to timezone wise.

    If you encounter more issues just modify the conditionals that deal with the month and day diff.

  18. PERFECT !!!!!

    Thank YOU !

    Work´s fine now !!!!!

  19. No problem, thanks for the debugging

  20. OHHHH NOOOOOO !!!! DON´t work fine !!!

    When i set the birthday to :

    14. Juni 1990 - shows : 21 years old

    16. Juni 1990 . shows : 20 years old

    16. Juli (July) 1990 shows : 20 years old

    16. August 1990 : shwos : 20 years old

    16. September 1990 : shows : 20 years old

    OK but now :

    when i set

    14.August 1990 : Shows 21 years old

    14. September 1990 : Shows 21 yaers old.

    TODAY IS THE 15.JUNI 2011 - what´s going wrong ????

    PLEASE HELP MEEEEEE !!!!!!

  21. or easyer :

    i am 41 years old , my birthday is : 17. 07. 1969 / 17.Juli 1969

    when i set my birthday to 14. Juli 1969 i am 42 yaers old

    when is set to 17. Juli 1969 , i am 41 years old

    when i set to 14. August 1969 , i am 42 years old

    when i set to 20. August 1969 , i am 41 years old

    OHHHHHHHHH MENNNNNN, what´s going on, my useres ask me, some questionßs i can not explane.... puhhhhh

    please help me again

  22. here is what the profile field give out :

    <label for="_day">

    <select name="_day" id="_day">

    <select name="_month" id="_month">

    <select name="_year" id="_year">

  23. comes from the "datebox"

  24. i test i with get_birthday : and i get it the right :

    when i set my birthday to : 19. Juni 1969 i get June 19 1969 that´s right

    what go wrong with age ????

  25. I ran the script again on my test site and you come up as 41 years old. The script may not be correct to the hour just the day.(depending on your timezone on the server)

    It tests ok, i wish i could help you out more but I need to go to work, good luck.

  26. OK !

    I hope you can help me more when you come back from work.

    I live in Germany,

    we have this : 17.Juli (July) 1969 not in America : July, 17 , 1969

    Should be this the problem?

    I use the TDLC - Birthday Plugin and it works right, but show´s the age of the birthday only in the widget.

    I use your snipset to show on member header and member-loop

    Please help me again and get it to work....

    what can i do?

    Funny is, when i set the day to 14 or 13 or 12 or 11 then i get the age 41

    when i set the day to 16, 17, 18, 19 or mo i get the age 42

    the month is April, or June or July, or August.

    Seems so, that the prg. look ONLY to the Day, not to the month......

    PLEASE HELP ME ! i am not soooo good in programing.....

    thank you very very much

  27. Thanks for the clue, that might be the key and i will try to help you figure it out when I get a chance.

  28. YES - please help me, because some of my users wonder why ......

    THANK YOU - i hope we get it

  29. I am at a loss, you may need to look for another snippet. All of the code on this site is experimental and I do my best to help out the coders but sometimes debugging this stuff requires first hand access to the files and hours and hours of tinkering. I just dont have any spare time at the moment.

  30. Uhhh :-(

    Would you please try - your snipset is the only one i found in the www

    or somebody from here ? can help me?

    thank you !

  31. HI again - sorry

    i look the hole day around in the wide world auf web (www) and found nothing to help me.

    what can i do?

    th only thing is - i would like to show the age of my users ....

    You have a idear?

    I use the TDLC - Birthday Plugin which can show the age when someone have birthday, and i try to put this in my member-loop but i have no luck.

    i am not a programmer, i know very little html and i am lost now...

    Please help me to get it work !
    THANK YOU SOOOOO MUCH

  32. I try again... but i habe noooooo luck !

    do you know someone who can help me with my problem ???

  33. Hello There,

    Thank you for the code. I was able to add it successfully to my member's loop. However, I'm have some trouble adding it to the member-header.php file. This is the code I'm using:

    @

    Perhaps, I have placed in the wrong place.

    Thank you.

  34. this is an obscure one, been a couple years since I used the functions. BP has made many updates since then. Sorry, trial and error may be your best bet, let me know if you figure it out.

  35. Hello,

    Was wondering is it possible to use this snippet and have the age show up in one section of the profile but in the users profile that has all the fields from the registration shown have the birth date?

    Thanks!

  36. I don't see why not. I have been out of the Buddypress game for a while so cant offer much more advice. Haven't used it since BP 1.3.

  37. Hi Adam,

    nice code, but I have a small issue, how can I call the age beside the date month year?

    Currently my user profile page is:

    Name | some name
    Birthday | October 4, 1984 (How can I display the age beside here?)

    Thank you

  38. Calling the function omega_get_age() between the parenthesis should do it. This is ancient code. I would actually be surprised if this is still working with the latest BP. I wrote this when it was at ver 1.0. - Good luck

  39. Hi Adam
    firstly thanx for your help out to WP sufferers. There re not many helpful guys like you around.
    My question is this:
    I have the latest Wp and BuddyPress, On the registration form I only want 1 form element appears (regarding to age) and that is AGE select box. (no day, month or year, just age). So users can only enter their age. And other users can only search age. So registration form and search form should have only age box. How can I do that? What files to change and how could you please explain and help me out on that please.
    Thanx.

  40. I wish I could help more oscar. I haven't used BP in a couple years now and I am way behind. Check out the forums at WPMUDEV, there is a lot of BP stuff there. http://premium.wpmudev.org/manuals/buddypress-2/

Comments and Feedback