[Resolved] Display age instead of date

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 2 years, 1 month ago.

Assigned support staff: Minesh.

Author
Posts
#617999

Hi. I have created a date field in a CPT called WI Groups. When the user adds the birth day/year of a group, it is displayed as 'Founded 1996'. I need it to say (22) as in 22 years old. How can I create a database query and View which displays this?

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<table width="100%" class="wpv-loop js-wpv-loop">
<wpv-loop wrap="2" pad="true">
[wpv-item index=1]
<tr>
<td>
[wpv-post-body view_template="Loop item in WI page list"]
</td>
[wpv-item index=other]
<td>
[wpv-post-body view_template="Loop item in WI page list"]
</td>
[wpv-item index=2]
<td>
[wpv-post-body view_template="Loop item in WI page list"]
</td>
</tr>
[wpv-item index=pad]
<td></td>
[wpv-item index=pad-last]
<td></td>
</tr>
</wpv-loop>
</table>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

Template for this View

[wpv-post-link] - Founded [types field='year-founded' style='text' format='Y'][/types]

Page where View output can be seen: hidden link

#618127

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - as I understand, you want to subtract your year field value from the current year and display the difference as result - correct? If yes:

Please add following code to your current theme's functions.php file:

add_shortcode( 'calculate_year', 'func_calculate_year');
function time_ago_func($atts){
    global $wpdb;
    extract( shortcode_atts( array(
    'birthdate' => '',
    ), $atts ) );
  
      $age = floor((time() - $birthdate)/(365*24*60*60));
     return $age;
}

Call the above shortcode inside your Template  :

[wpv-post-link] - ( [calculate_year birthdate='[types field="year-founded" raw="true"][/types]'] )