User:Barthax~metawiki/DateTime Template Function
Template:Extension Created a new function template: {{DATETIME:<date>[|<format>]}} taking a format from the PHP strftime date->string conversion routine.
Usage
[edit]- Template function: {{datetime:
- Date/time to be converted: 1/1/1980 11:23
- Optional Parameter: |%D %T
- Close template: }}
Example: {{datetime:1/1/1980 11:23pm|%D %T}} would generate 01/01/80 23:23:00.
File modifications
[edit]Based on v1.5.6.
includes/DefaultSettings.php
[edit]At the end:
$wgDateFormatDefault = "%D %T";
language/Languages.php
[edit]Add another magic word to the end of the array:
MAG_DATETIME => array( 0, 'DATETIME:' ),
includes/MagicWord.php
[edit]Add a new definition for the generated MAG_DATETIME to the end of the definitions of MAG_*:
define('MAG_DATETIME', 44);
Obviously increase the numeric to be one more than it's predecesor.
includes/Parser.php
[edit]The guts of the mod, all done in the braceSubstitution function.
Add the default date format to the globals:
global $wgLinkCache, $wgContLang;
becomes:
global $wgLinkCache, $wgContLang, $wgDateFormatDefault;
Further down, add the following after the check for GRAMMAR:
# DATETIME # Format a date/time parameter via http://php.net/manual/en/func tion.strftime.php format if ( !$found ) { # Check for DATETIME: (namespace expansion) $mwDate = MagicWord::get( MAG_DATETIME ); if ( $mwDate->matchStartAndRemove( $part1 ) ) { $dateParsed = strtotime($part1); if ($dateParsed == FALSE ) { $text = $linestart . "Invalid Date/Time: " . $part1; $found = true; } else { if ( $argc == 0 ) { $text = $linestart . $debug . strftime( $wgDateFormatDefault , $dateParsed); } else { $text = $linestart . $debug . strftime( $args[0] , $dateParsed); } $found = true; } } }
Conclusion
[edit]This may not look especially useful at first but was created for a template being used on a private wiki which requires the same date being expressed in multiple locations but in different formats. Hence a template such as the following can be created (the original is rather more extensive):
Birthdate: {{datetime:{{{birthdate}}}|%D %T}} [[Category:{{datetime:{{{birthdate}}}|%p}}]] [[Category:{{datetime:{{{birthdate}}}|%Y}}]] [[Category:{{datetime:{{{birthdate}}}|%B}}]]