public
Created

Get Age from BuddyPress datebox field data

  • Download Gist
bpdev_get_age_from_dob
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/**
* Get Age from BuddyPress date of Birth
* @param string $dob_field_name :name of the DOB field in xprofile, like Dob or Date of Birth
* @param int $user_id : the user for which you want to retrieve the age
* @param string $format: the way you want to print the difference, look t <http://php.net/manual/en/dateinterval.format.php> 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);
}
 
 
 
/*usage
 
echo "Your Age".bpdev_get_age_from_dob("Dob",bp_loggedin_user_id());
//and so on
 
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.