User:AndreasBlixt/Sandbox/Countdown extension

An extension which counts down to a specified date.

Usage
This extension is used by adding a tag in the format, for example.

is the date and/or time which will be counted down to. The date/time can be in almost any format imaginable, such as  or even.

means that it will only show the weeks and days left until the specified date/time. This can be any combination of the following, seperated by a space:,  ,  ,   and.

may also be used, which limits the amount of pieces the countdown will consist of ( in this example). So if it would have shown, with   it would show.

Output
The output will look something like: 2 weeks, 1 day, 6 hours, 10 minutes, 8 seconds

If the given date/time has expired or there was another error, it will return an empty string.

Code
setHook("countdown", "renderCountdown"); }

function renderCountdown($input, $argv) { global $wgParser;

// Get the time we're counting down to   if ((int)($time = strtotime($input)) <= 0) return "";

// Get the difference from now to then if (($seconds = $time - time) <= 0) return "";

// Set up array of time unit data $units = array(       array(604800, 'week' , 'weeks'  ),        array( 86400, 'day'   , 'days'   ),        array(  3600, 'hour'  , 'hours'  ),        array(    60, 'minute', 'minutes'),        array(     1, 'second', 'seconds'),    );

// Handle arguments if (isset($argv["pieces"])) $pieces = (int)$argv["pieces"]; else $pieces = 0; if (isset($argv["show"])) { $temp    = explode(" ", $argv["show"]); $newunits = array; foreach ($temp as $timeunit) { foreach ($units as $unit) { if (in_array($timeunit, $unit)) $newunits[] = $unit; }       }        $units = $newunits; }

// Initialize variables to be used $count = 0; $string = "";

// Loop through the units and add the ones which fit onto the string that // will be returned foreach ($units as $data) { $length = $data[0];

// This unit can fit the current remainder of seconds if ($seconds >= $length) { $amount  =  floor($seconds / $length); $seconds -= $length * $amount;

// Separate each item with a comma if ($string) $string .= ', ';

// Add the current unit in either singular or plural depending on           // whether the value is 1 or not if ($amount == 1) { $string .= $amount. ' ' . $data[1]; } else { $string .= $amount. ' ' . $data[2]; }

// Stop if the maximum amount of pieces to show is reached if (++$count == $pieces) break; }   }

// Return the formatted string return $string; } ?>

Installation
Save the above code into, then edit   and add   to the bottom (above the  ).