-
-
Save labsecrets/2379377 to your computer and use it in GitHub Desktop.
Get Age from BuddyPress datebox field data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This now works on both members list and individual profile pages. Note that "birthdate" is name of profile date field - change as needed | |
* Get Age from BuddyPress date of Birth | |
* <a href='http://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='http://buddypress.org/community/members/param/' rel='nofollow'>@param</a> int $user_id : the user for which you want to retrieve the age | |
* <a href='http://buddypress.org/community/members/param/' rel='nofollow'>@param</a> 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($birthdate,$user_id=false,$format="%y Years, %m Month(s), %d days"){ | |
if(!$user_id) | |
$user_id=bp_get_member_user_id(); | |
$dob_time=xprofile_get_field_data($birthdate, $user_id);//get the datetime as myswl datetime | |
$dob = new DateTime($dob_time); | |
$today = new DateTime(); | |
$diff = round(abs($today->format('U') - $dob->format('U')) / (60*60*24*365)); | |
return $diff; | |
} | |
/*usage | |
<?php echo "<strong>Age:</strong> ".bpdev_get_age_from_dob("birthdate"); ?> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment