• Hi,

    I have a list of babies born less than a year ago registered. UM only displays age in years for each registration. We need to be able to show the age of under 1 year old in months.

    I understand there is a element of %s years, is there a code for %months?

    Is this possible?

    Thanks,

    Denis

    • This topic was modified 3 years, 10 months ago by den1sa.
    • This topic was modified 3 years, 10 months ago by den1sa.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @den1sa

    Please try this code snippets that enables the birth date with months:

    add_filter( 'um_profile_field_filter_hook__date', 'um_041921_profile_field_filter_hook__date', 89, 2 );
    function um_041921_profile_field_filter_hook__date( $value, $data ) {
    
    	if ( ! $value ) {
    		return '';
    	}
        
        $then_ts = strtotime( $value );
        $then_year = date( 'Y', $then_ts );
        $age = date( 'Y' ) - $then_year;
        if ( strtotime( '+' . $age . ' years', $then_ts ) > current_time( 'timestamp' ) ) {
            $age--;
        }
        if ( $age >= 1 ) {
            return $value;
        }
    
        if ( $age <= 0 ) {
            $month = date("n", strtotime( $value ) );
            $datetime1 = date_create( date("Y-m-d", strtotime( $value ) ) );
            $datetime2 = date_create( date("Y-m-d", current_time( 'timestamp' ) ) );
            $interval = date_diff($datetime1, $datetime2);
            remove_filter( 'um_profile_field_filter_hook__date', 'um_profile_field_filter_hook__date', 99, 2 );
            
            $month = $interval->format("%m");
    
            return sprintf("%d ",(int)$month)._n( 'month old', 'months old', (int)$month, 'ultimate-member'  );
        }
    
    	return $value;
    }

    You can add this code snippet in your theme’s functions.php file or use the Code Snippets plugin to run the code.

    Regards,

    • This reply was modified 3 years, 10 months ago by Champ Camba.
    Thread Starter den1sa

    (@den1sa)

    Hi Champ.

    I used Code Snippet to insert this code. Thank you.

    It worked!

    Denis

    • This reply was modified 3 years, 10 months ago by den1sa.
    Thread Starter den1sa

    (@den1sa)

    Now. On the age selection filter for searches, how can I search for registered users say between 3 to 6 months old?

    • This reply was modified 3 years, 10 months ago by den1sa.
    Thread Starter den1sa

    (@den1sa)

    Back to the original code. I have just tried to make a slight amendment so that months are displayed for under 2 year olds. But unfortunately this code doesn’t seem to recognize the year of birth and bases the age on the current year.

    So, D.O.B. = 1st Nov 2019 shows 5 months old.

    I amended this part only.

     if ( $age >= 2 ) {
            return $value;
        }
    
        if ( $age < 2 ) {

    See code below.

    add_filter( 'um_profile_field_filter_hook__date', 'um_041921_profile_field_filter_hook__date', 89, 2 );
    function um_041921_profile_field_filter_hook__date( $value, $data ) {
    
    	if ( ! $value ) {
    		return '';
    	}
        
        $then_ts = strtotime( $value );
        $then_year = date( 'Y', $then_ts );
        $age = date( 'Y' ) - $then_year;
        if ( strtotime( '+' . $age . ' years', $then_ts ) > current_time( 'timestamp' ) ) {
            $age--;
        }
        if ( $age >= 2 ) {
            return $value;
        }
    
        if ( $age < 2 ) {
            $month = date("n", strtotime( $value ) );
            $datetime1 = date_create( date("Y-m-d", strtotime( $value ) ) );
            $datetime2 = date_create( date("Y-m-d", current_time( 'timestamp' ) ) );
            $interval = date_diff($datetime1, $datetime2);
            remove_filter( 'um_profile_field_filter_hook__date', 'um_profile_field_filter_hook__date', 99, 2 );
            
            $month = $interval->format("%m");
    
            return sprintf("%d ",(int)$month)._n( 'month old', 'months old', (int)$month, 'ultimate-member'  );
        }
    
    	return $value;
    }
    
    Thread Starter den1sa

    (@den1sa)

    I suppose the ideal is;

    If Birth Date is 18th Oct 2019 then should appear as 18 months old.
    If 2 years old or over then it appears as normal years.

    Thanks in advance.

    Denis

    • This reply was modified 3 years, 10 months ago by den1sa.
    Thread Starter den1sa

    (@den1sa)

    BUMP!

    Can anyone help?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @den1sa

    Please try this code snippet with your changes.

    add_filter( 'um_profile_field_filter_hook__date', 'um_041921_profile_field_filter_hook__date', 89, 2 );
    function um_041921_profile_field_filter_hook__date( $value, $data ) {
    
    	if ( ! $value ) {
    		return '';
    	}
        
        $then_ts = strtotime( $value );
        $then_year = date( 'Y', $then_ts );
        $age = date( 'Y' ) - $then_year;
        if ( strtotime( '+' . $age . ' years', $then_ts ) > current_time( 'timestamp' ) ) {
            $age--;
        }
        if ( $age >= 2 ) {
            return $value;
        }
    
        if ( $age < 2 ) {
            $month = date("n", strtotime( $value ) );
            $datetime1 = date_create( date("Y-m-d", strtotime( $value ) ) );
            $datetime2 = date_create( date("Y-m-d", current_time( 'timestamp' ) ) );
            $interval = date_diff($datetime1, $datetime2);
            remove_filter( 'um_profile_field_filter_hook__date', 'um_profile_field_filter_hook__date', 99, 2 );
            
            $month = $interval->format("%m") + 12;
             
            return (int)$month." "._n( 'month old', 'months old', (int)$month, 'ultimate-member'  );
        }
    
    	return $value;
    }
    

    I’ve only added the changes in $month = $interval->format("%m") + 12;

    Regards,

    Thread Starter den1sa

    (@den1sa)

    Thanks Champ.

    Worked like a charm.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Display Age in Years and Months’ is closed to new replies.