Age Calc in TWIG or PHP

Hi,
I fully understand that PHP custom code isn’t covered by Theme.co support, but since the issue is similar when using TWIG, I hope you can guide me on how to resolve it in any of these ways.

I have a Custom Post Type “Specialist” with a number of fields. There are two date fields used one to dynamically calculate a person’s current age and the second for years of experience. When I integrate a PHP function or TWIG template to accomplish this, I encounter an unusual issue. Some of the values in the array are processed and calculated properly, but others return an error. I’ve been trying to change date format but none of them worked out. Currently, ‘d/m/Y’ format is used everyware (Word Press defaults for the date format as well as respective ACF fields). What am I doing wrong?

I will provide details on the implementation for TWIG as it is an officially supported feature.

{% set birth_date = acf.post_field({"field":"specialist_birthdate"}) %}
{% set difference = date(endDate).diff(date(birth_date)) %}
{% set age = difference.y %}
	{{ age }}

The code for years of experience is almost the same. Here is what I get for some of the records on the front-end (if PHP function is used I get blank page for the specialist where an error persist):

And here what I get inside Cornerstone:

Here is the link to the specialists archive page for preliminary check of some more examples.

Hello John,

Thanks for writing in!

If the endDate is empty, it may prompt an error. Try adding a condition like for example:

{% set birth_date = acf.post_field({"field":"specialist_birthdate"}) %}
{% if birth_date is not empty and endDate is defined %}
    {% set difference = date(endDate).diff(date(birth_date)) %}
    {% set age = difference.y %}
    {{ age }}
{% else %}
    {# Display a fallback message or nothing #}
    Date information unavailable.
{% endif %}

Hope this helps.

Hi @ruenel,

Thank you very much for providing the complete syntax. However, the question is still the same. What is the reason behind the specialist_birthdate field consistently causing an exception? The fallback message serves its purpose effectively now if that is the case. However, in my case, all respective CPT records have values and are marked as required in the ACF field group. Which means that initially, every CPT record contains required date value, but the calculations for some cases still have an exception which is confusing. There appears to be a syntax error in the ‘if else’ statement. Removing it allows all ‘set’ statements to return the correct values.

{% set birthDate = acf.post_field({"field":"specialist_birthdate"}) %}
	Birth date is {{ birthDate }} and
{% set endDate = date()|date('d/m/Y') %}
  current date is {{ endDate }}.
{% if birthDate is not empty and endDate is defined %}
	{% set difference = date(endDate).diff(date(birthDate)) %}
	{% set age = difference.y %}
    Age equals to {{ age }} full years
{% else %}
    {# Display a fallback message or nothing #}
    (Date information unavailable)
{% endif %}  

On top of that, there is another question. Some of the strings are not calculated correctly and show figures for one year less. This one, for example. If we compare the current date and the birthdate, it should be 60 not 59.