Support

Account

Home Forums Front-end Issues Display age from birth date

Solving

Display age from birth date

  • Hello,

    I try to set up the automatic display of a person’s age, same way as Wikipedia does.

    exemple : 17 mai 1990 (33 ans)

    here is my code:

    
    <?php echo get_field( 'person-birthday', $post->ID ); ?> //display the date
    <?php $date_string = get_field( 'person-birthday', $post->ID ); 
    $date = DateTime::createFromFormat( 'Ymd', $date_string ); 
    $birthday = new DateTime( $date ); 
    $interval = $birthday->diff( new DateTime ); 
    echo '(' .$interval->y.' ans)'; ?>
    

    Source: https://support.advancedcustomfields.com/forums/topic/calculate-age-from-date-field/#post-45149

    My issue is that the age always display “0”. Is there something wrong in my code ?

  • Hello, I still need help on this one, thanks.

  • when you use get_field() it will return the date in the format you specified for “Return Format”, make sure this matches with the format you are trying to use when creating the DateTime object.

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

You must be logged in to reply to this topic.