Skip Navigation

[Resolved] Shortcode for date calculations

This support ticket is created 6 years, 9 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by romanB-3 6 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#555352

Hello,

I have a custom post type with these 2 custom fields :
ORDERDATE = date of the order
DAYS = number of days

Based on those, I need to calculate a deadline, that will display in views : DEADLINE = ORDERDATE + DAYS.

I wonder what shortcode I should use (and possibly customize) to make it work.

Thank you.

#555475

Dear roman,

There isn't such a existed shortcode works for your case,
I suggest you create another new shortcode for it:
https://codex.wordpress.org/Function_Reference/add_shortcode
1) Get the values of custom field "ORDERDATE" and "DAYS"
https://codex.wordpress.org/Function_Reference/get_post_meta

2) Calculate the value as what you want:
DEADLINE = ORDERDATE + DAYS.

#555975

Thank you, here it is if anyone needs it :

/**************************************************
 * Shortcode pour calculer le délai effectif de livraison d'une commande
 **************************************************/
function calculer_delaireel_de_livraison( ) {
	$post_id = get_the_ID();
	
	$date_commande = get_post_meta($post_id,'wpcf-date-de-la-contractualisation-de-la-commande', true);
	$date_livraison = get_post_meta($post_id,'wpcf-date-de-livraison-de-la-commande', true);
	$delaireel = ($date_livraison - $date_commande)/(60 * 60 * 24);
	if ($delaireel < 1) {
		return "le jour même" ;
	}
	elseif ($delaireel < 2) {
		return "en " . round($delaireel) . " jour" ;
	}
	else {
		return "en " . round($delaireel) . " jours" ;
	}
}
add_shortcode( 'delai-reel', 'calculer_delaireel_de_livraison' );
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.