Search
Other Articles Anywhere questions
Forum

Age Calculation

Alex R's Avatar Alex R
Hello Peter,

I am using Articles Anywhere to pull the birthday data from a custom field and I would like to perform a calculation so that it shows the age of the person in the article. My question is, if this is something I can accomplish with Articles Anywhere alone?

Thanks!

Alex
Peter van Westen's Avatar Peter van Westen ADMIN
No, Articles Anywhere doesn't support that level of calculations.
You would have to do that via PHP, for instance via Sourcerer.
Please post a rating at the Joomla! Extensions Directory
Alex R's Avatar Alex R
I am trying the following, but it is not pulling the value from the custom field.
{source}<?php
function calculateAge($birthdate) {
    $today = new DateTime();
    $birthDate = new DateTime($birthdate);
    $age = $today->diff($birthDate)->y;
    return $age;
}

$birthdate = "{article}[date-of-birth]{/article}"; 
$age = calculateAge($birthdate);
echo "Current Age: " . $age . " years";
?>
{/source}

The custom field being date-of-birth

What am I missing?
Peter van Westen's Avatar Peter van Westen ADMIN
Please try the latest development version from:
regularlabs.com/development-releases

You can then disable the protection inside Sourcerer tags in the Articles Anywhere system plugin settings (under Advanced tab).

This should also work:
{article}{source}<?php echo date("Y")-[date-field format="Y"]-( date("nd") < [date-field format="nd"] ? 1 : 0 ); ?>{/source}{/article}

PS: Make sure the Sourcerer system plugin is ordered after Articles Anywhere.
See: regularlabs.com/blog/242-plugin-order-is-important
Please post a rating at the Joomla! Extensions Directory
Alex R's Avatar Alex R
Thank You Peter!

I uploaded the developer version, but I do not see any new option to disable the protection. See below images.

zwdrive.com/5psFSp
zwdrive.com/tG5wXA

I also tried your code suggestion, but it doesn't work.
Peter van Westen's Avatar Peter van Westen ADMIN
That setting is in Articles Anywhere, not Sourcerer.
Please post a rating at the Joomla! Extensions Directory
Alex R's Avatar Alex R
Thanks Peter, this works now with the new setting in Articles Anywhere.
Peter van Westen's Avatar Peter van Westen ADMIN
Ok, great 🙂
The new setting will be included in the next release.
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription