RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (5.84%*) • Fixed: 5.39% (5.84%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.19%*) • Investment PI: 5.55% (6.02%*)

Determine A Person’s Age From DOB (With PHP Or WordPress Shortcode)

Determine A Person’s Age From DOB (With PHP Or WordPress Shortcode)

One of the sites we worked with back in 2015 would render the age of a person (or years since an event) in the article body. Since much of the information we share online is timeless in nature it makes sense to always have a dynamic (and always up-to-date) age calculation. The WordPress shortcode function on this page will show you how to include the current age of a person (or years since a any event) with very simple WordPress shortcode. There's a number of these little countdown snippets we'll be sharing over time - this is just the first.

The Result

To display the current age of Jennifer Lawrence we would use the shortcode of [age dob="19900815"] (15th August, 1990. See below for other date options). The result: 34. If you're somebody that prefers opening and closing tags, consider using [age]19900815[/age]. Returns: 15th August 1990 (age: 26).

There is a caveat in providing shortcodes that support both the opening and closing tags version: you must use one or the another. Using both on the same page will cause erroneous output to be rendered.

WordPress Shortcode Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Determine A Person's Age From DOB (With PHP Or WordPress Shortcode)
4
 http://www.beliefmedia.com/determine-age-from-dob
5
*/
6
 
7
function beliefmedia_determine_age($atts, $content = null) {
8
  extract( shortcode_atts( array(
9
    'dob' => '', /* See post for date formats */
10
    'date' => 0,
11
    'dateformat' => 'jS F Y' /* http://php.net/manual/en/function.date.php */
12
  ), $atts ) );
13
 
14
 if ($dob == '') $dob = $content;
15
 $age = ($content == null) ? floor((time() - strtotime($dob)) / 31556926) : floor((time() - strtotime($content)) / 31556926);
16
 return ($date) ? date($dateformat, strtotime($dob)) . ' (age: ' .  $age . ')' : $age;
17
}
18
add_shortcode('age', 'beliefmedia_determine_age');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

While there's a plugin download below, the custom functions option is a far more reasonable one. Our plugin options are usually there to mitigate issues with entry-level WordPress users, and this is one of those examples.

Shortcode Attributes

The following shortcode attributes may be used.

dob

The DOB is the date of birth in the format (preferably) YYYYMMDD. Refer below for more information. You can provide this as either an attribute or wrapped in opening and closing age tags. Use one method or the other; not both.

date

If you would like to display the date in addition to the age, use date="1". The default means of rendering the date and age is 15th August 1990 (age: 26). Adjust the code as necessary to alter the output.

dateformat

If you want to display the date in addition to the age, the dateformat determines how the date will be written. Refer to the PHP manual for information.

PHP Function

Used outside of WordPress, the following can be used.

1
<?php 
2
/*
3
 Determine A Person's Age From DOB (With PHP Or WordPress Shortcode)
4
 http://www.beliefmedia.com/determine-age-from-dob
5
*/
6
 
7
function beliefmedia_determine_age($dob) {
8
  return floor((time() - strtotime($dob)) / 31556926);
9
}
10
 
11
/* Usage: 5th August, 1990 */
12
$dob = '19900815';
13
14
?>

Date Formats

PHP's strtotime() function recognises only certain patterns and it's easy to confuse date formats because of the backwards way the USA represents the date. The American m/d/y is assumed with slashes; however, if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD). In my example above I've used the 6 figure time group of YYYYMMDD (without any separator). An English representation is okay as well. For example, [age dob="25 December 2005"] will also work.

Considerations

  • Showing a real-time age can often be a little deceiving if shown in the context of a dated page. Many will look to a publication date and make their own age correction (which will, of course, be in error). We've only used the functionality on a platform that showed a current age on a page where publication date was irrelevant so we really haven't encountered our own issues.
  • As previously stated, you should use a shortcode version with the closing tags or the one without - don't use both. The shortcode parser is unable to handle mixing of enclosing and non-enclosing forms of the same [$tag].
  • If you're a client and require this feature, please get in touch.

Video Introduction

Download


Title: Determine A Person's Age From DOB (Shortcode)
Description: Show the current age of a person (or years since a any event) with WordPress shortcode or PHP.
  Download • Version 0.1, 578.0B, zip, Category: WordPress Shortcodes
WordPress Plugins (General), (706.0B)    PHP Code & Snippets, (427.0B)    

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment