ez.no / ezpublish / documentation / customization / tips & tricks / find the age in years for an object
These documentation pages are no longer maintained. Please visit the new documentation site.
If you have an user, or any content object with an datefield, then you can use the following code to find the age in years for the object. Note the object must have an datefield called "born", or you must replace born in the code below with the name of your datefield.
{* Calculate the age for the user, find the difference in years, month and day from the current date *}
{let curr_ts=currentdate()
age=sub($curr_ts|datetime( custom, '%Y'),$node.object.data_map.born.data_int|datetime( custom, '%Y'))
res_m=sub($curr_ts|datetime( custom, '%m'),$node.object.data_map.born.data_int|datetime( custom, '%m'))
res_d=sub($curr_ts|datetime( custom, '%d'),$node.object.data_map.born.data_int|datetime( custom, '%d'))
}
{* age is now simply the difference in year, next is some logic to find out the difference relative to current month and day *}
{section show=$res_m|lt(0)}
{* Month, Less than zero, subtract one *}
{set age=$:age|dec}
{/section}
{section show=and($res_m|eq(0),$res_d|lt(0))}
{* Month is equal, check days, if less than zero subract one from year *}
{set age=$:age|dec}
{/section}
AGE IS {$age}
{/let}
Comments
Thanks !!!
Patrick ALLAERT
Saturday 08 October 2005 2:48:16 pm