Skip to:
Content
Pages
Categories
Search
Top
Bottom

Calcuate Age on Member-loop.php?

  • How do you calculate age in the member-loop.php?

    I’ve got the age to calculate in member-header.php with this code

    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;
    }

     

    And outputting with

    <<?php echo “Age:”.bpdev_get_age_from_dob(“birthday”); ?>

     

    Does not seem to work

    when I place this code in the member-loop.php

Viewing 3 replies - 1 through 3 (of 3 total)

  • shanebp
    Moderator

    @shanebp

    You’re sure that $dob is being returned correctly ?
    Then try this right in the loop:

    `
    $dob = xprofile_get_field_data($birthdate, bp_get_member_user_id());
    $birthdate = explode(” “, $dob);
    $birthdate = birthdate[0];
    $birthDate = explode(“/”, $birthDate);
    //get age from birthdate
    $age = (date(“md”, date(“U”, mktime(0, 0, 0, $birthDate[1], $birthDate[2], $birthDate[0]))) > date(“md”) ? ((date(“Y”)-$birthDate[0])-1):(date(“Y”)-$birthDate[0]));
    echo “Age is:”.$age;`

    There is lots of google info available re php datetime -> age

    the profile field name is set as “Birthday”

    Could that be the problem?

    with the current codes I am using

    the age only seems to convert if I input “birthday” instead of “birthdate”

    for the member-header.php

     

     


    modemlooper
    Moderator

    @modemlooper

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Calcuate Age on Member-loop.php?’ is closed to new replies.
Skip to toolbar