Extension:Strtotime

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
Strtotime

Release status: stable

Implementation Parser function
Description Allows the represention of time as a string containing a US English date format.
Author(s) Xavier Atero (xaviTalk)
Last version 1.0 (2010-07-29)
MediaWiki tested on 1.15.3
PHP tested on 5.2.13
License GPL
Download No link
Hooks used
ParserFirstCallInit

LanguageGetMagic

Check usage (experimental)

Contents

[edit] Purpose

This extension allows to represent time as a string containing a US English date format.

Note: Same functionality can be achieved using #time from ParserFunctions Extension.

[edit] Usage

{{#strtotime: time transformation | year | month | day | format}}

where:

  • 'time transformation' is the string containing time transformation applied to the base date to calculate the target date
  • 'year' is the base year to calculate the target date
  • 'month' is the base month to calculate the target date
  • 'day' is the base day to calculate the target date
  • 'format' is the string containing the target date output format

[edit] Installation

  1. Copy source code into a file called $IP/extensions/Strtotime/Strtotime.php
  2. Enable the extension by adding this line to LocalSettings.php:
    require_once("$IP/extensions/Strtotime/Strtotime.php");
    

[edit] Examples

 {{#strtotime:tomorrow|2000|1|1|Y-m-d}}
 Output: 2000-01-02
 {{#strtotime:+1 day|2000|1|1|Y-m-d}}
 Output: 2000-01-02
 {{#strtotime:yesterday|2000|1|1|Y-m-d}}
 Output: 1999-12-31
 {{#strtotime:+1 week 2 days|1981|4|10|l dS F Y}}
 Output: Sunday 19th April 1981
 {{#strtotime:next Thursday|2000|1|1|d M Y (l)}}
 Output: 06 Jan 2000 (Thursday) 
 {{#strtotime:last Monday|2000|1|1|d M Y (l)}}
 Output: 27 Dec 1999 (Monday) 

If not otherwise specified the target date time will be 00:00:00.

 {{#strtotime:+1 day 4 hours 32 min|2000|1|1|c}}
 Output: 2000-01-02T04:32:00-08:00

If the transformation time is a date the output will be such date

 {{#strtotime:28 May 1976|2000|1|1|F d, Y}}
 Output: May 28, 1976

The word 'now' refers to the base date and not to the current date.

 {{#strtotime:now|2000|1|1|Y-m-d}}
 Output: 2000-01-01

When the base day exceeds the number of days of a the base month, the offset days will be interpreted as days of the next month.

 {{#strtotime:+1 day|1979|2|33|F d, Y}}
 Output: March 06, 1979

Syntax errors in the time transformation will generated undetermined outputs.

 {{#strtotime:+1 month|2000|1|1|M/d/Y}}
 Output: Jan/02/2000

Check PHP's date() function for complete description of date formats.

Check PHP's datetime formats for complete description of available time transformations.

[edit] Source Code

<?php
/**
 * @copyright Copyright © 2010, Xavier Atero.
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, version 2
 * of the License.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * See the GNU General Public License for more details.
 */
 
/**
 * Strtotime
 * @package MediaWiki
 * @subpackage Extensions
 * @author Xavier Atero
 *
 * This extension allows to represent time as a string containing a US English date format.
 * The syntax is the following:
 * {{#strtotime: time transformation | year | month | day | format}}
 * where 'time transformation' is the string containing time transformation applied
 *                             to the base date to calculate the target date
 * 'year' is the base year to calculate the target date
 * 'month' is the base month to calculate the target date
 * 'day' is the base day to calculate the target date
 * and 'format' is the string containing the target date output format
 * 
 *
 * @version $Revision: 1.0 $
 */
 
# Alert the user that this is not a valid entry point to MediaWiki
# if they try to access the special pages file directly.
if (!defined('MEDIAWIKI')) {
        echo <<<EOT
To install Strtotime extension, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/Strtotime/Strtotime.php" );
EOT;
        exit( 1 );
}
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Strtotime',
        'author' => 'Xavier Atero',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Strtotime',
        'description' => 'Represents time as a string containing a US English date format',
        'descriptionmsg' => 'Represents time as a string containing a US English date format',
        'version' => '$Revision: 1.0 $',
);
 
# Define a setup function
$wgHooks['ParserFirstCallInit'][] = 'efStrtotime_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]  = 'efStrtotime_Magic';
 
/**
 * Registers the extension with the WikiText parser by setting a
 * function hook associating the "strtotime" magic word with 
 * the callback function.
 *
 * @param &$parser instance of the WikiText parser
 * @return true
 */
function efStrtotime_Setup( &$parser ) {
        $parser->setFunctionHook( 'strtotime', 'efStrtotime_Render' );
        return true;
}
 
/**
 * Populates the magicWords array with the magic word 
 * 'strtotime' used by this extension.
 * 
 * @param &$magicWords reference to the array to populate
 * @param $langCode w:ISO 639-2 letter language code
 * @return true
 */
function efStrtotime_Magic( &$magicWords, $langCode ) {
        $magicWords['strtotime'] = array( 0, 'strtotime' );
        return true;
}
 
/**
 * The callback function returns the target date after applying the
 * time transformation to the base date.
 *
 * @param &$parser instance of the WikiText parser
 * @param $time_transformation string containing time transformation 
 *        applied to the base date to calculate the target date
 * @param $year base year to calculate the target date
 * @param $month base month to calculate the target date
 * @param $day base day to calculate the target date
 * @param $format string containing the target date output format
 * @return target date
 */
function efStrtotime_Render ( &$parser, $time_transformation , $year, $month, $day, $format = 'Y-m-d' ) {
        $base_date = mktime( 0, 0, 0, $month, $day, $year );
        $target_date = date( $format, strtotime( $time_transformation , $base_date ) ); 
        return $target_date;
}
?>
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox