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