RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Static Day Countdown with WordPress Shortcode or PHP

Static Day Countdown with WordPress Shortcode or PHP

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: 8 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 7 years. When an expiry has passed (using our last webinar as an example), it will show as passed (7 years 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.

1
<?php 
2
/*
3
 Static Day Countdown with WordPress Shortcode or PHP
4
 http://www.beliefmedia.com/static-countdown
5
*/
6
 
7
function beliefmedia_static_countdown($atts) {
8
 
9
  extract(shortcode_atts(array(
10
    'text' => 'Passed',
11
    'url' => false,
12
    'style' => 'text-decoration; none;',
13
    'date' => '',
14
    'offset' => false,
15
  ), $atts));
16
 
17
    /* If no date */
18
    if ($date == '') $date = date('Y') . '1231235959';
19
 
20
    /* Date timestamp */
21
    $date = strtotime($date);
22
 
23
    /* WP or defined offset */
24
    $offset = ($offset !== false) ? $offset : get_option('gmt_offset');
25
 
26
    /* UTC offset */
27
    $r_date = ($offset !== false) ? (strpos($offset,'-') !== false) ? bcadd($date, abs((3600 * $offset))) : bcsub($date, abs((3600 * $offset))) : $date;
28
 
29
    /* Total seconds to event */
30
    $remain = $r_date - time();
31
 
32
 /* Render result */
33
 $result = ($remain < 0) ? $text . ' (' . human_time_diff(time(), $r_date) . ' ago)' : human_time_diff(time(), $r_date);
34
 $result = ($url !== false) ? '<a href="' . $url . '" title="' . date('D jS F Y, g:ia', $date) . '">' . $result . '</a>' : '<abbr title="' . date('D jS F Y, g:ia', $date) . '">' . $result . '</abbr>';;
35
 
36
 /* Result */
37
 return '<span style="' . $style . '">' . $result . '</span>';
38
}
39
add_shortcode('days', 'beliefmedia_static_countdown');

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().

1
<?php 
2
/*
3
 Static Day Countdown with WordPress Shortcode or PHP
4
 http://www.beliefmedia.com/static-countdown
5
*/
6
 
7
function beliefmedia_static_countdown($date = '', $offset = '', $text = 'Passed') {
8
 
9
    /* If no date */
10
    if ($date == '') $date = date('Y') . '1231235959';
11
 
12
    /* Offset to 0 if not set */
13
    $offset = ($offset != '') ? $offset : 0;
14
 
15
    /* Date timestamp */
16
    $date = strtotime($date);
17
 
18
    /* UTC offset */
19
    if ($offset != '0') $r_date = (strpos($offset,'-') !== false) ? bcadd($date, abs((3600 * $offset))) : bcsub($date, abs((3600 * $offset)));
20
      else $r_date = $date;
21
 
22
    /* Total seconds to event */
23
    $remain = $r_date - time();
24
 
25
 /* Render result */
26
 $result = ($remain < 0) ? $text . ' (' . beliefmedia_human_time_diff(time(), $r_date, false) . ')' : beliefmedia_human_time_diff(time(), $r_date, false);
27
 $result = '<abbr title=&quot;' . date('D jS F Y, g:ia', $date) . '&quot;>' . $result . '</abbr>';
28
 
29
 /* Result */
30
 return '<span style=&quot;' . $style . '&quot;>' . $result . '</span>';
31
}
32
 
33
/* Usage */
34
echo beliefmedia_static_countdown($date = '20th june 2017 9.45am', $offset = '0');

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 in WordPress (WP Plugin)
Description: Display a countdown to an event in readable text with shortcode.
  Download • Version 0.2, 1.6K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (782.0B)    PHP Code & Snippets, (743.0B)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment