Skip Navigation

[Resolved] Display age instead of date

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 2.48 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 6 years, 2 months ago.

Assisted by: 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  :
[php]
[wpv-post-link] - ( [calculate_year birthdate='[types field="year-founded" raw="true"][/types]'] )
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.