MediaWiki  1.34.0
DateFormatter Class Reference

Date formatter. More...

Public Member Functions

 __construct (Language $lang)
 
 reformat ( $preference, $text, $options=[])
 

Static Public Member Functions

static getInstance (Language $lang=null)
 Get a DateFormatter object. More...
 

Public Attributes

const ALL = -1
 Used as a preference ID for rules that apply regardless of preference. More...
 
const DM = 6
 e.g. More...
 
const DMY = 2
 e.g. More...
 
const ISO = 4
 e.g. More...
 
const LAST = 7
 The highest ID that is a valid target format. More...
 
const LASTPREF = 4
 The highest ID that is a valid user preference. More...
 
const MD = 7
 e.g. More...
 
const MDY = 1
 e.g. More...
 
const NONE = 0
 No preference: the date may be left in the same format as the input. More...
 
const YDM = 5
 e.g. More...
 
const YMD = 3
 e.g. More...
 

Private Member Functions

 makeIsoMonth ( $monthName)
 Makes an ISO month, e.g. More...
 
 makeIsoYear ( $year)
 Make an ISO year from a year name, for instance: '-1199' from '1200 BC'. More...
 
 makeNormalYear ( $iso)
 Make a year from an ISO year, for instance: '400 BC' from '-0399'. More...
 

Private Attributes

string[] $monthNames = []
 Month names by number. More...
 
int[] $preferenceIDs
 A map of descriptive preference text to internal format ID. More...
 
string[] $regexes
 Date format regexes indexed the class constants. More...
 
int[][] $rules = []
 Array of special rules. More...
 
string[] $targetFormats
 Format strings similar to those used by date(), indexed by ID. More...
 
int[] $xMonths = []
 Month numbers by lowercase name. More...
 

Detailed Description

Date formatter.

Recognises dates and formats them according to a specified preference.

This class was originally introduced to detect and transform dates in free text. It is now only used by the {{#dateformat}} parser function. This is a very rudimentary date formatter; Language::sprintfDate() has many more features and is the correct choice for most new code. The main advantage of this date formatter is that it is able to format incomplete dates with an unspecified year.

Definition at line 37 of file DateFormatter.php.

Constructor & Destructor Documentation

◆ __construct()

DateFormatter::__construct ( Language  $lang)
Parameters
Language$langIn which language to format the date

Definition at line 103 of file DateFormatter.php.

References $lang, $monthNames, ALL, DM, DMY, ISO, MD, MDY, NONE, and YMD.

Member Function Documentation

◆ getInstance()

static DateFormatter::getInstance ( Language  $lang = null)
static

Get a DateFormatter object.

Deprecated:
since 1.33 use MediaWikiServices::getDateFormatterFactory()
Parameters
Language | null$langIn which language to format the date Defaults to the site content language
Returns
DateFormatter

Definition at line 169 of file DateFormatter.php.

References $lang.

◆ makeIsoMonth()

DateFormatter::makeIsoMonth (   $monthName)
private

Makes an ISO month, e.g.

02, from a month name

Parameters
string$monthNameMonth name
Returns
string|false ISO month name, or false if the input was invalid

Definition at line 294 of file DateFormatter.php.

Referenced by reformat().

◆ makeIsoYear()

DateFormatter::makeIsoYear (   $year)
private

Make an ISO year from a year name, for instance: '-1199' from '1200 BC'.

Parameters
string$yearYear name
Returns
string ISO year name

Definition at line 307 of file DateFormatter.php.

Referenced by reformat().

◆ makeNormalYear()

DateFormatter::makeNormalYear (   $iso)
private

Make a year from an ISO year, for instance: '400 BC' from '-0399'.

Parameters
string$isoISO year
Returns
int|string int representing year number in case of AD dates, or string containing year number and 'BC' at the end otherwise.

Definition at line 325 of file DateFormatter.php.

Referenced by reformat().

◆ reformat()

DateFormatter::reformat (   $preference,
  $text,
  $options = [] 
)
Parameters
string$preferenceUser preference, must be one of "default", "dmy", "mdy", "ymd" or "ISO 8601".
string$textText to reformat
array$optionsIgnored. Since 1.33, 'match-whole' is implied, and 'linked' has been removed.
Returns
string

Definition at line 183 of file DateFormatter.php.

References $source, ALL, LAST, makeIsoMonth(), makeIsoYear(), makeNormalYear(), and NONE.

Member Data Documentation

◆ $monthNames

string [] DateFormatter::$monthNames = []
private

Month names by number.

Definition at line 57 of file DateFormatter.php.

Referenced by __construct().

◆ $preferenceIDs

int [] DateFormatter::$preferenceIDs
private

A map of descriptive preference text to internal format ID.

Definition at line 62 of file DateFormatter.php.

◆ $regexes

string [] DateFormatter::$regexes
private

Date format regexes indexed the class constants.

Definition at line 39 of file DateFormatter.php.

◆ $rules

int [][] DateFormatter::$rules = []
private

Array of special rules.

The first key is the preference ID (one of the class constants), the second key is the detected source format, and the value is the ID of the target format that will be used in that case.

Definition at line 47 of file DateFormatter.php.

◆ $targetFormats

string [] DateFormatter::$targetFormats
private

Format strings similar to those used by date(), indexed by ID.

Definition at line 65 of file DateFormatter.php.

◆ $xMonths

int [] DateFormatter::$xMonths = []
private

Month numbers by lowercase name.

Definition at line 52 of file DateFormatter.php.

◆ ALL

const DateFormatter::ALL = -1

Used as a preference ID for rules that apply regardless of preference.

Definition at line 68 of file DateFormatter.php.

Referenced by __construct(), and reformat().

◆ DM

const DateFormatter::DM = 6

e.g.

15 January

Definition at line 92 of file DateFormatter.php.

Referenced by __construct().

◆ DMY

const DateFormatter::DMY = 2

e.g.

15 January 2001

Definition at line 77 of file DateFormatter.php.

Referenced by __construct().

◆ ISO

const DateFormatter::ISO = 4

e.g.

2001-01-15

Definition at line 83 of file DateFormatter.php.

Referenced by __construct().

◆ LAST

const DateFormatter::LAST = 7

The highest ID that is a valid target format.

Definition at line 98 of file DateFormatter.php.

Referenced by reformat().

◆ LASTPREF

const DateFormatter::LASTPREF = 4

The highest ID that is a valid user preference.

Definition at line 86 of file DateFormatter.php.

◆ MD

const DateFormatter::MD = 7

e.g.

January 15

Definition at line 95 of file DateFormatter.php.

Referenced by __construct().

◆ MDY

const DateFormatter::MDY = 1

e.g.

January 15, 2001

Definition at line 74 of file DateFormatter.php.

Referenced by __construct().

◆ NONE

const DateFormatter::NONE = 0

No preference: the date may be left in the same format as the input.

Definition at line 71 of file DateFormatter.php.

Referenced by __construct(), and reformat().

◆ YDM

const DateFormatter::YDM = 5

e.g.

2001, 15 January

Definition at line 89 of file DateFormatter.php.

◆ YMD

const DateFormatter::YMD = 3

e.g.

2001 January 15

Definition at line 80 of file DateFormatter.php.

Referenced by __construct().


The documentation for this class was generated from the following file: