Quite often in a WordPress post you’ll talk about an event and give a specific countdown to the number of days until that event will arrive. The problem? Well, the day after you’ve written the post, the information becomes invalid and out-of-date. Wouldn’t it be nice to include a countdown in days to a specific event that is always current? Wouldn’t it be nice to have a countdown that will dynamically count down the number of days and be updated every time somebody reads your post – regardless of when they read it? Well, with shortcode, this can be easily accomplished.
For example, it’ll be ...woops,NYE has passed..., 2011. That countdown will cycle down every day until the event has arrived, at which time it’ll note that the event has passed.
Copy and paste the following into your theme’s functions.php file.
function countdown_event($atts, $content = null)
{
extract(shortcode_atts(array(
'event' => '',
'month' => '',
'day' => '',
'year' => ''
), $atts));
// subtract desired date from current date and give an answer in terms of days
$remain = ceil( ( mktime( 0,0,0,$month,$day,$year ) - time() ) / 86400 );
// show the number of days left
if( $remain > 0 )
{
$daysleft = "<strong>$remain</strong> more days until $event";
}
// Event has arrived!
else
{
$daysleft = "...woops,$event has passed...(or some other message in here)";
}
return $daysleft;
}
add_shortcode('countdown', 'countdown_event');
To use the shortcode, you’ll have to call it in the following fashion from within your post (using the HTML editor, of course). You can change the messages as you wish or delete them entirely and simply output the days remaining.
[countdown event="NYE" month="12" day="31" year="2011"]
|
|
If you liked this article, you may also like:
- Twitter Style Time: “Posted n Seconds/Minutes/Hours/Days Ago” (and WordPress Shortcode to Reference Other Posts)
- Display Future (Scheduled) WordPress Posts with Shortcode
- [Shortcode] Easily post an aviation TAF report into your WordPress post or page with shortcode
- [Shortcode] Easily post an aviation Metar report into your post or page with shortcode
- [Shortcode] Examples [2]


[...] time back, I provided code that would ‘dynamically’ count down days until an event. The following is far more [...]