Skip Navigation

[Resolved] Display Post Date minus one year

This thread is resolved. Here is a description of the problem and solution.

Problem:
I need to display both the date of the post and the date minus one year of the post, within the same view.

Solution:
1. Add following code in your theme’s functions.php file:

add_shortcode('past_year_date', 'get_past_year_date_fun');
function get_past_year_date_fun( $atts ) {
    // Attributes
    extract(shortcode_atts(
        array(
            'date' => '',
    'format' => 'M d, Y'
        ),
        $atts
    ));
     
    $past_date = strtotime("$date -1 year");
    $date = date($format, $past_date);
    return $date;
}

2. Then use following shortcode for display past year date:

Current Date: [wpv-post-date] 
Past Year Date: [past_year_date date="[wpv-post-date]" format="M d, Y"]
This support ticket is created 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Adrian 7 years, 2 months ago.

Assisted by: Noman.

Author
Posts
#562505

Hi,

I have a post, published on Aug 22 2017. If I wish to output the year the post was published in, I can do so easily via [wpv-post-date format="Y"] which gives me 2017.

What shortcode or combination would give me 2016 (the previous year)

#562518

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Change post  date.png

Hi Adrian,

Thank you for contacting Toolset support. There is an easier way to do that. Edit the post and change the “Published on” date of the post by clicking the “Edit” link, see attached screenshot.

Otherwise custom shortcode needs to be created to achieve this.

Thank you

#562558

Hi Norman,

I'm afraid you missed the point.

I need to display both the date of the post and the date minus one year of the post, within the same view.

#562682

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Okay, I am trying to achieve this using custom shortcode and I will update you shortly with my findings shortly.

Thank you for your patience.

#562694

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

1. Please add following code in your theme’s functions.php file:

add_shortcode('past_year_date', 'get_past_year_date_fun');
function get_past_year_date_fun( $atts ) {
    // Attributes
    extract(shortcode_atts(
        array(
            'date' => '',
	'format' => 'M d, Y'
        ),
        $atts
    ));
	
	$past_date = strtotime("$date -1 year");
	$date = date($format, $past_date);
	return $date;
}

2. Then use following shortcode for display past year date:

Current Date: [wpv-post-date] 
Past Year Date: [past_year_date date="[wpv-post-date]" format="M d, Y"]

I hope this is what you are looking for. Thank you

#565424

That's it!
Thank you very much for your help 🙂