Display php date calculation, with a shortcode

Trying to display “XX Year in the Industry” text on a CPT. The idea is that the client would enter the year the staff member started in a custom field and an automatic calculation would subtract that # from the current year and display.

The basic math is: Current year – Entered start year = Displayed year

This is almost working… but the calculation doesn’t happen. The shortcode works, but not the calculation. Am I missing a filter? Do I maybe need 2 functions, one for the shortcode and one for the calc?

function YearsIndustry_shortcode($metadata, $object_id, $meta_key, $single) {
   $start_year = get_post_meta( $post_id, 'ptb_start_year' );
   $current_year = new DateTime("now");
   $displayed_year = date_diff($current_year, $start_year)->y;
   $Years = $displayed_year. 'Years in Industry';
   return $Years;
add_shortcode('YII', 'YearsIndustry_shortcode');

Read more here: Display php date calculation, with a shortcode

Leave a Reply

Your email address will not be published. Required fields are marked *