and like us on FB

Further to a shortcode we shared that would render a dynamic countdown with JavaScript, the following code will display a static countdown to an event. Unlike the previous version of the same code where we only counted down to the number of days (previously published on Internoetics), this counter will count down to a specific time. Using WordPress’ human_time_diff() function, we’ll render human-readable strings to your target time.

The Result

By default, and without any attributes, the shortcode will display the countdown to NYE. For example, [days] returns: 3 months. Because we’re using WP’s human readable functions, the rendering isn’t overly descriptive. For example, Christmas day in 2030 (using [days date="25th December 2030"] will show as 13 years. When an expiry has passed (using our last webinar as an example), it will show as passed (3 months ago). The text of ‘passed’ is determined by a shortcode attribute (I’ve added del tags to mine).

To link your result, use the url attribute. The full date will be returned in the HTML title attribute.

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Shortcode Attributes

The following shortcode attributes are available.

date

The date must be a valid strtotime() compatible string.

text

The text is what will be rendered when your time has passed.

url

If the url attribute is used, we’ll wrap a link around the result (the title attribute will be the full readable date/time).

style

Use style="something: something;" to style the result.

offset

If the offset is not defined, we’ll use your offset as stored in your WP database. We use offset="10" (for Sydney).

PHP Function

A PHP function to be used outside of WordPress is as follows. Usage requires beliefmedia_human_time_diff().

The offset (in hours) is required when a correction must be applied to your resulting result. To test, set a time one hour in the future and note the difference. The way of determining the offset is a little counter-intuitive (since the intent is manufacture a UTC offset). A negative offset will mean the value will be added to your result.

Considerations

Download

Title: Static Day Countdown with WordPress Shortcode or PHP
Description: Display a countdown to an event in readable text with shortcode.
Download: Shortcode (V0.2) | WP Plugin (V0.2) | PHP Code (V0.2)

Shortt URL for this post: http://shor.tt/2gYB