Support

Account

Home Forums Front-end Issues Display age from birth date Reply To: Display age from birth date

  • <?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)'; 
        }
    }
    ?>