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.

  • <?php 
    // Get the raw field value (ACF date field usually stored as Ymd, e.g. 19901231)
    $date_string = get_field( 'person-birthday', $post->ID ); 
    
    if( $date_string ) {
        // Convert to DateTime object
        $birthday = DateTime::createFromFormat( 'Ymd', $date_string ); 
    
        if( $birthday ) {
            // Calculate age
            $interval = $birthday->diff( new DateTime() ); 
    
            // Display formatted date + age
            echo $birthday->format('d M Y') . ' (' . $interval->y . ' ans)'; 
        }
    }
    ?>
  • Hi jnrmy,

    You’re very close! The issue is in your DateTime::createFromFormat() call. You’re using the format ‘Ymd’ which expects a string like “19900517”, but ACF date fields by default return the date as “Ymd” format – which is exactly what you’re getting from get_field().

    The problem is you’re then trying to create a new DateTime object from the result, but $date is already a DateTime object. Here’s the corrected code:

    php
    <?php
    $date_string = get_field(‘person-birthday’, $post->ID);
    if($date_string) {
    $birthday = DateTime::createFromFormat(‘Ymd’, $date_string);
    $today = new DateTime();
    $interval = $birthday->diff($today);

    // Display the original date in your preferred format
    echo $birthday->format(‘d M Y’); // Shows “17 May 1990”

    // Display the age
    echo ‘ (‘ . $interval->y . ‘ ans)’;
    }
    ?>
    Key fixes:

    Removed the redundant new DateTime($date) – $date is already a DateTime object

    Added a check to ensure the date field isn’t empty

    Used $birthday->format() to display the date in a readable format

    Created $today as a separate DateTime object for the comparison

    Alternative simpler approach:
    If you prefer a more concise version:

    php
    <?php
    $birthday = get_field(‘person-birthday’, $post->ID);
    if($birthday) {
    $age = date_diff(date_create($birthday), date_create(‘today’))->y;
    echo date(‘d M Y’, strtotime($birthday)) . ‘ (‘ . $age . ‘ ans)’;
    }
    ?>
    The main issue was creating a DateTime object from another DateTime object, which was resetting the date. Try the first solution and it should display the correct age!

    Let me know if you’re still seeing “0” after this fix.

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

You must be logged in to reply to this topic.