Posted on by

Showing a Person’s Age in a Record

I get this request a lot, so I thought I’d write up a tutorial on how to do this.

Note: this tutorial was updated in Dec 2019 to work with current versions of Participants Database.

The idea is, given someone’s birth date, show how old they are today. This same idea can be used to show all kinds of current data based on saved data, such as now many days it will be until someone’s birthday, etc..

The best way to do this sort of thing is to write a custom plugin. I know that sounds intimidating to some, but this will be a relatively simple plugin, and I’ll include the code so you don’t even have to write it…although you will have to modify it a little in order for it to work in your particular setup.

I’m stressing that because I don’t want anyone to simply install the plugin I’m providing and then come back and say it doesn’t work!

In this simple plugin, all we’re doing is making sure the field shows the person’s current age. This doesn’t mean you’ll be able to do searching and sorting by age (you have to use the birth date field for that) because the record in the database is only updated with the age value if the record is saved again.

Making sure the age value is always current in the record is a bit more complicated, and this plugin doesn’t do that.

Defining the Age Field

First step is to create an age field. Go to the plugin’s Manage Database Fields page and create up your age field as a numeric field with an appropriate name (I used “age”). Make it read-only since you don’t want to set it manually.

Setting the Age Field’s Value

We will be using the pdb-before_field_added_to_iterator action to set the field’s value in a display context.

In a form context (when the record is getting edited) we use the pdb-form_element_build_{$element} action, which in our case is pdb-form_element_build_numeric because our age field is a numeric type field.

Check the Participants Database API for the details on those actions.

The Plugin

Here is the code, with comments. It will seem fairly complex to those unfamiliar with the use of PHP classes, but all you really need to know is how to edit the values of the two properties (variables) at the top of the class.

To install the plugin, take a look at this: How to Install a WordPress Plugin from a Gist

5 thoughts on “Showing a Person’s Age in a Record

  1. OK.
    Thanx anyway!

  2. Hello Rolan!
    I custumized your PDb Show Participant’s age plugin, but it doesn’t work.
    Breaks up the page.
    The Query Monitor plugin errror:

    Uncaught Exception: DateTime::__construct(): Failed to parse time string (@) at position 0 (@): Unexpected character

    .
    Any help would be appreciated.
    Elvis

    1. I don’t recognize the error, not sure where it’s coming from. I’m sorry, but generally speaking you’re going to be on your own with this code, I am providing some custom code examples, it is expected that you have the knowledge to use the code and adapt it to you uses. I don’t provide free support for that,

  3. Would it be possible to have multiple ages in the same record? I host a personnel database for a not-for-profit organization. Each record (or volunteer) has multiple tests and classes that we must keep track of. I need to have a way to keep track the “age” of each test (the time since the test was taken) to notify our volunteers when their tests are due.

    Another possibility is to have a field to calculate the due date for each test (Test1_Due=Test1+3 Years). For example, if a test was taken on Jan. 31 2020, the next test would be due Jan. 31 2023.

    Would either of these be possible? Preferably both.

    1. Hi Donald,

      Certainly possible. A lot depends on what you needed to do with the information, for example, you could do all of this in a custom template if you only needed to show these values to the user. If you wanted to initiate actions, that would require something more like a plugin that ran on a cron. You’ll need to have some pretty good coding skills to figure out how it needed to work and set all that up, not something I would be able to provide with a few lines of code like this article.

Leave a Reply

Your email address will not be published. Required fields are marked *