Manual:Dynamic dates

The date formatting feature‎ is long ago implemented in includes/DateFormatter.php, later moved to includes/parser/DateFormatter.php. All you need to do is enable $wgUseDynamicDates in LocalSettings.php:

$wgUseDynamicDates = true;

Original proposal


This is the original proposal by Tim Starling, who coded it. It was inspired by discussion on w:Wikipedia talk:Manual of Style (dates and numbers) (archive).

A new user preference is added, allowing the user to select their preferred date format. The default option is not to impose any style on the text, but to simply leave it as typed by the editor. To reduce the pressure on editors to conform to the article naming standard, redirects are skipped with an effective pipe. This scheme requires that a standard naming convention be hard-coded. It is intended that this restriction be removed, or somehow made less visible, with a future modification. For example, the titles of the 366 date articles could magically change depending on user preferences.

Here's a few bits of sample text, which show the effective operation taking place.

Assuming the user prefs are set to "23 June 2003" style:

  • [[June 23]] → [[June 23|23 June]]
  • [[23 June]] → [[June 23|23 June]]
  • [[2003]] [[June 23]] → [[June 23|23 June]] [[2003]]
  • [[1000 BC]] [[June 23]] → [[June 23|23 June]] [[1000 BC]]

Some kinds of badly formatted dates should be recognised and standardised.

  • [[june 23]] → [[June 23|23 June]]
  • [[June 23]],[[2003]] → [[June 23|23 June]] [[2003]] (missing space)

In date links with pipes or trailing characters, the target should be changed but not the link text:

  • [[23 June|23rd of June]] → [[June 23|23rd of June]]
  • [[June 23]]rd → [[June 23]]rd

Regular expressions


Now for specifics, with Perl regular expressions. Skip this bit if you don't understand it, it's not very important. We label links to the following article titles as type MD:

 )[ _](\d{1,2})$/ix

And similarly for DM

/^(\d{1,2})[ _](

Years Y can be defined as follows:

/^(\d{1,4}[ _](BC|)$/

To convert the text, we effectively perform these operations:

  1. Convert full dates. No piped text allowed. Full dates are defined as a link of type DM or MD on either the left or the right of a link of type Y, possibly separated by a comma and arbitrary whitespace. For example: "MD,Y", "DM, Y", "Y MD", "YMD". The year, month and day is extracted, and the date reconstructed based on preferences. If there is no preference, it is converted to one of the three standard formats: "DM, Y", "MD, Y" or "Y MD", whichever one is most like the source text. This makes accidental full date creation more obvious, and fixes simple formatting errors.
  2. Convert MD and DM links to the preference. Piped text is allowed and preserved, with only the target changed as described above. Note that reprocessing of dates processed in step 1 should have no effect.

See also meta:Spacetime DTD