---
title: "Calculating a person&#8217;s age based on a date field"
date: 2026-04-23
author: "GravityKit"
link: "https://www.gravitykit.com/docs/gravityview/advanced/calculating-a-persons-age-based-on-a-date-field/"
---

Ever wanted to calculate someone else's age based on a date field? That's possible using the `[gv_age]` shortcode.

### Shortcode Parameters

The `[gv_age]` shortcode requires two parameters to calculate age.

- `entry_id` - The ID of the entry for the age calculation. Can use the `{entry_id}` Merge Tag or a numeric entry ID.
- `field_id` - The ID of the date field where date of birth is stored.

### Example

This shortcode will calculate the ages for all entries. Date of birth is stored in a date field with an ID of 44:

```

[gv_age entry_id="{entry_id}" field_id="44" /]
```

```

[gv_age entry_id="{entry_id}" field_id="44" /]
```

Adding the `[gv_age]` shortcode to a [Custom Content](https://www.gravitykit.com/docs/gravityview/getting-started-gravityview/using-the-custom-content-field/) field:

![Replace with your date field ID for age calculation shortcode](https://www.gravitykit.com/wp-content/uploads/2025/10/file-gt20qq6h13.png)
*The output:*![Table showing names, birth dates, and custom ages calculated: Amy 20, Randall 34, Chris 25](https://www.gravitykit.com/wp-content/uploads/2025/10/file-MD53y0xbsw.png)
*Now that we have the age displayed, we can enter a label for the Custom Content field:*![GravityView setup to calculate age from a date field with custom label option](https://www.gravitykit.com/wp-content/uploads/2025/10/file-aEMxIj5a5q.png)
*And voilá:*![Table with names, birth dates, and ages: Amy, Randall, Chris; ages: 20, 34, 25](https://www.gravitykit.com/wp-content/uploads/2025/10/file-j11M62eYrr.png)
You'll need this code to enable the `[gv_age]` shortcode:
```

add_shortcode( 'gv_age', 'gv_calculate_age' );

function gv_calculate_age( $atts ) {

	$defaults = array(
		'field_id'    => '',
		'entry_id'    => '',
		'format'      => '%y',
		'hide_errors' => ''
	);

	$atts = shortcode_atts( $defaults, $atts, 'gv_age' );

	$entry = GFAPI::get_entry( $atts['entry_id'] );

	if ( ! $entry || is_wp_error( $entry ) ) {
		return empty( $atts['hide_errors'] ) ? 'Error: Entry not found' : '';
	}

	if ( empty( $entry[ $atts['field_id'] ] ) ) {
		return empty( $atts['hide_errors'] ) ? 'Error: Field value not specified.' : '';
	}

	$from = new DateTime( $entry[ $atts['field_id'] ] ); // Birth date
	$to   = new DateTime( 'now' );
	$interval = $from->diff( $to );

	return $interval->format( $atts['format'] ); // Default format is years ('%y')
}
```

```

add_shortcode( 'gv_age', 'gv_calculate_age' );

function gv_calculate_age( $atts ) {

	$defaults = array(
		'field_id'    => '',
		'entry_id'    => '',
		'format'      => '%y',
		'hide_errors' => ''
	);

	$atts = shortcode_atts( $defaults, $atts, 'gv_age' );

	$entry = GFAPI::get_entry( $atts['entry_id'] );

	if ( ! $entry || is_wp_error( $entry ) ) {
		return empty( $atts['hide_errors'] ) ? 'Error: Entry not found' : '';
	}

	if ( empty( $entry[ $atts['field_id'] ] ) ) {
		return empty( $atts['hide_errors'] ) ? 'Error: Field value not specified.' : '';
	}

	$from = new DateTime( $entry[ $atts['field_id'] ] ); // Birth date
	$to   = new DateTime( 'now' );
	$interval = $from->diff( $to );

	return $interval->format( $atts['format'] ); // Default format is years ('%y')
}
```

Read here how to add these code samples to your website: [Where to put code samples.](https://www.gravitykit.com/docs/gravityview/customizing-your-views/where-to-put-code-samples/)