Jump to content

User:Barthax~metawiki/DateTime Template Function

From Meta, a Wikimedia project coordination wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Template:Extension Created a new function template: {{DATETIME:<date>[|<format>]}} taking a format from the PHP strftime date->string conversion routine.

Usage

  • 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

Based on v1.5.6.

includes/DefaultSettings.php

At the end:

$wgDateFormatDefault = "%D %T";

language/Languages.php

Add another magic word to the end of the array:

        MAG_DATETIME             => array( 0,    'DATETIME:'              ),

includes/MagicWord.php

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

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

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}}]]