Skip Navigation

[Resolved] Display Countdown using Date Field

This support ticket is created 5 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 6.08 hours from now. Thank you for your understanding.

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/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by jackC-7 5 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1345871

Tell us what you are trying to do?
We have set up a Custom Post Type with a repeatable section for courses. In these repeatable sections we have date fields for when the course is out of date. We would like a countdown for each course that will say 45 days to go or if it has passed to say Expired 12 days ago.

Is this something that is possible?
Thanks

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1346121

Hi Jack,

Thank you for waiting.

Based on what you've shared, you can update the code snippet from the other ticket, to include the number of days comparison, like this:


add_shortcode("show_day_difference", "show_day_difference_func");
function show_day_difference_func($atts) {

	$a = shortcode_atts( array(
		'date' => ''
	), $atts );

	// today's date
	$current_date = date( 'U' );

	// date to compare
	$compare_date = $a['date'];
	
	// difference in dates
	$datediff = $compare_date - $current_date;

	// difference in number of days
	$datediffdays = round($datediff / (60 * 60 * 24));

	// conditional display of difference based on number of days 
	if( ($datediffdays == 0) || ($datediffdays == 1) ) {
		$output = '<div class="date-diff-orange">Expires in 1 day</div>';
	}
	elseif ($datediffdays == -1) {
		$output = '<div class="date-diff-red">Expired 1 day ago</div>';
	}
	elseif ($datediffdays < -1) {
		$output = '<div class="date-diff-red">Expired '.abs($datediffdays).' days ago</div>';
	}
	elseif ( ($datediffdays >= 2) && ($datediffdays <= 190) ) {
		$output = '<div class="date-diff-orange">Expires in '.$datediffdays.' days</div>';
	}
	elseif ( $datediffdays > 190 ) {
		$output = '<div class="date-diff-green">Expires in '.$datediffdays.' days</div>';
	}

	return $output;
}

The above code snippet can be added into the active theme's "functions.php" file and this is how you can pass on the date field's value in the shortcode:


[show_day_difference date="[types field='date-field-slug' output='raw'][/types]"]

Please replace "date-field-slug" with the actual slug of the date field used on your website and note that shortcode will output the result, inside special classes, based on the number of days e.g. "date-diff-orange", "date-diff-green" & "date-diff-red", which can be used to style them differently.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1346147
Screenshot 2019-09-23 at 13.01.44.png

Hi there,

I have added the code into functions.php and the shortcode into the view.

But they all say Expired 18163 days ago (see attached)

Have I done something wrong?

#1346189

Hi Jack,

Can you please share temporary admin login details and the link to a page where this view can be seen?

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1346231

Hi Jack,

Thank you for sharing the login details.

I've noticed that you're using the new shortcode in the view "04 - Qualification List" and two items, need to be fixed:

1. In the view's "Content Selection" section, please select "Qualification Upload", instead of "Qualifications".
( screenshot: hidden link )

2. In the "Loop item in 04 - Qualification List" section, please update the correct slug of the date field "qualification-expiry-date".
( screenshot: hidden link )

regards,
Waqar

#1360309

Hi there,

Thank you for sorting this... I was just wondering if there was a way that if the date is not set It will hide the HTML code?

Thanks,
Jack