Display Age in Years and Months
-
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
- The topic ‘Display Age in Years and Months’ is closed to new replies.