Skip Navigation

[Résolu] Display Post Date minus one year

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 Il y a 7 années et 3 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Marqué : ,

Ce sujet contient 5 réponses, a 2 voix.

Dernière mise à jour par Adrian Il y a 7 années et 3 mois.

Assisté par: Noman.

Auteur
Publications
#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

Les langues: Anglais (English )

Fuseau horaire: 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

Les langues: Anglais (English )

Fuseau horaire: 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

Les langues: Anglais (English )

Fuseau horaire: 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 🙂