• Resolved den1sa

    (@den1sa)


    Hi,

    I had a previous post about 1 year ago. Thanks to Champ I was able to move off the start line.

    https://www.ads-software.com/support/topic/display-age-in-years-and-months/#post-14350552

    However, there is unfortunately an issue with this code. Let me explain.

    So todays date is 12th June 2022. When I enter the date of birth as…

    1st May 2022 displays as ′13 months old′ should be ′1 month old′
    11th April 2022 displays as ′14 months old′ should be ′2 months old′
    1st Jan 2022 displays as ′17 months old′ should be ′5 months old′
    25th Dec 2020 displays as ′17 months old′ this correctly displays the months.

    So you can see that if the child is under 12 months old, the script adds 12 months to the age, if over 12 months old the display age is correct.

    Also, when I add a child under 2 years old, the display ages of all older people above 2 years old appear as date of birth and no longer the age.

    Please help debug this script. Thanks in advance.

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

    Denis

    • This topic was modified 2 years, 9 months ago by den1sa.
    • This topic was modified 2 years, 9 months ago by den1sa.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter den1sa

    (@den1sa)

    Hi ,

    I think I almost cracked this one. It works if you have a person registered in each age group, so…

    < 1, <2 and >=2

    If i do not have a person to fit each age group the age for the >=2 shows as 52 years old.

    Please help.

    Thanks,

    Denis

    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 ) {
     
    	if ( isset( $data['pretty_format'] ) && $data['pretty_format'] == 1 ) {
    		$value = UM()->datetime()->get_age( $value );
    	} else {
    		$format = empty( $data['format_custom'] ) ? $data['format'] : $data['format_custom'];
    		$value = date_i18n( $format, strtotime( $value ) );
            
    		return $value;
    	}	
    		
    	}  if ( $age < 1 ) {
            $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") + 0;
             
            return (int)$month." "._n( 'month old', 'months old', (int)$month, 'ultimate-member'  );
    		
    	}  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;
    
    }
    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hi @den1sa

    Please try this updated code snippet which will give you “days old”, “weeks old”,”months old”, “years old” values for “birth_date” field:

    add_filter( 'um_profile_field_filter_hook__date', 'um_061421_profile_field_filter_hook__date', 80, 2 );
    function um_061421_profile_field_filter_hook__date( $value, $data ) {
       
    	if ( ! $value ) {
    		return '';
    	}
        
        if( $data['metakey'] !== 'birth_date' ){
            return $value;
        }
    
        remove_filter( 'um_profile_field_filter_hook__date', 'um_profile_field_filter_hook__date', 99, 2 );
       
        
        $hours_in_day   = 24;
        $minutes_in_hour= 60;
        $seconds_in_mins= 60;
        $birth_date     = new DateTime( $value );
        $current_date   = new DateTime();
    
        $diff           = $birth_date->diff($current_date);
    
       $years     = $diff->y;
       $months    = ($diff->y * 12) + $diff->m;
       $weeks     = floor($diff->days/7);
       $days      = $diff->days;
       $hours     = $diff->h + ($diff->days * $hours_in_day);
       $mins      = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour);
       $seconds   = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins);
      
        if( $days < 7 ){
            return $days . ' ' . _n( 'day old', 'days old', $days, 'ultimate-member'  );
        }else if( $days >= 7 && $weeks < 4 ){
            return $weeks . ' ' . _n( 'week old', 'weeks old', $weeks, 'ultimate-member'  );
        }else if( $weeks >=4 && $months < 12 ){
            return $months . ' ' . _n( 'month old', 'months old', $months, 'ultimate-member'  );
        }else if( $months >= 12 || $years > 1 ){
            return $years . ' ' . _n( 'year old', 'years old', $years, 'ultimate-member'  );
        }
    
        return $value;
    }

    Replace the old code with the above.
    Let me know if you’re still having issues.

    Thread Starter den1sa

    (@den1sa)

    It worked perfectly. Thank you.

    However, the sort order seems to show months old, then days old. Is it possible to hard code sort by days, weeks, months and years old in that order?

    • This reply was modified 2 years, 9 months ago by den1sa.
    Thread Starter den1sa

    (@den1sa)

    I have tried this in the code, but it doesnt work.

    	$obj->query_args['orderby'] = array(
    		'wxo_featured_clause' => 'DESC',
                    'birth_date'          => 'ASC',
    	); 
    • This reply was modified 2 years, 9 months ago by den1sa.
    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hi @den1sa

    Please create a new topic for different issues. I’m marking this as resolved now

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