User:Lucamauri/Sandbox/ParserTime/en
Temporary page to improve documentation from https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##time as discussed in T223453
.
#time
[edit]
Code | Description | Current output (Purge this page's cache to update) |
---|---|---|
Year | ||
Y
|
4-digit year. | 2024 |
y
|
2-digit year. | 24 |
L
|
1 if it's a leap year, 0 if not. | 1 |
o [note 1]
|
ISO-8601 year of the specified week.[note 2] | 2024[note 3] |
Month | ||
n
|
Month index, not zero-padded. | 12 |
m
|
Month index, zero-padded. | 12 |
M
|
An abbreviation of the month name, in the site language. | Dec |
F
|
The full month name in the site language. | December |
xg
|
Output the full month name in the genitive form for site languages that distinguish between genitive and nominative forms. This option is useful for many Slavic languages like Polish, Russian, Belarusian, Czech, Slovak, Slovene, Ukrainian, etc. | For Polish:{{#time:F Y|June 2010|pl}} β czerwiec 2010(nominative) {{#time:d xg Y|20 June 2010|pl}} β 20 czerwca 2010(genitive) |
Day of the month or the year | ||
j
|
Day of the month, not zero-padded. | 3 |
d
|
Day of the month, zero-padded. | 03 |
z
|
Day of the year (January 1 = 0). Note: To get the ISO day of the year add 1. |
337 |
Week and day of the week | ||
W
|
ISO 8601 week number, zero-padded. | 49 |
N
|
ISO 8601 day of the week (Monday = 1, Sunday = 7). | 2 |
w
|
Number of the day of the week (Sunday = 0, Saturday = 6). | 2 |
D
|
An abbreviation for the day of the week. Rarely internationalized. | Tue |
l
|
The full weekday name. Rarely internationalized. | Tuesday |
Hour | ||
a
|
"am" during the morning (00:00:00 β 11:59:59), "pm" otherwise (12:00:00 β 23:59:59). | am |
A
|
Uppercase version of a above.
|
AM |
g
|
Hour in 12-hour format, not zero-padded. | 11 |
h
|
Hour in 12-hour format, zero-padded. | 11 |
G
|
Hour in 24-hour format, not zero-padded. | 11 |
H
|
Hour in 24-hour format, zero-padded. | 11 |
Minutes and seconds | ||
i
|
Minutes past the hour, zero-padded. | 18 |
s
|
Seconds past the minute, zero-padded. | 20 |
U
|
Unix time. Seconds since January 1 1970 00:00:00 GMT. | 1733224700 |
Timezone (as of 1.22wmf2) | ||
e
|
Timezone identifier. | UTC |
I
|
Whether or not the date is in daylight savings time. | 0 |
O
|
Difference to Greenwich time (GMT) | +0000 |
P
|
Difference to Greenwich time (GMT), with colon | +00:00 |
T
|
Timezone abbreviation. | UTC |
Z
|
Timezone offset in seconds. | 0 |
Miscellaneous | ||
t
|
Number of days in the current month. | 31 |
c
|
ISO 8601 formatted date, equivalent to Y-m-d"T"H:i:s+00:00 .
|
2024-12-03T11:18:20+00:00 |
r
|
RFC 5322 formatted date, equivalent to D, j M Y H:i:s +0000 , with weekday name and month name not internationalized.
|
Tue, 03 Dec 2024 11:18:20 +0000 |
Non-Gregorian calendars | ||
Islamic | ||
xmj
|
Day of the month. | 1 |
xmF
|
Full month name. | Jumada al-thani |
xmn
|
Month index. | 6 |
xmY
|
Full year. | 1446 |
Iranian (Jalaly) | ||
xit
|
Number of days in the month. | 30 |
xiz
|
Day of the year. | 258 |
xij
|
Day of the month. | 13 |
xiF
|
Full month name. | Azar |
xin
|
Month index. | 9 |
xiY
|
Full year. | 1403 |
xiy
|
2-digit year. | 03 |
Hebrew | ||
xjj
|
Day of the month. | 2 |
xjF
|
Full month name. | Kislev |
xjt
|
Number of days in month. | 30 |
xjx
|
Genitive form of the month name. | Kislev |
xjn
|
Month number. | 3 |
xjY
|
Full year. | 5785 |
Thai solar | ||
xkY
|
Full year in Thai solar calendar. Note: For years before 1941 the dates in Jan-Mar range are not calculated properly. |
2567 |
Minguo/Juche year | ||
xoY
|
Full year. | 113 |
Japanese nengo | ||
xtY
|
Full year. | 什ε6 |
Flags | ||
xn
|
Format the next numeric code as a raw ASCII number. | In the Hindi language, {{#time:H, xnH}} produces ΰ₯¦ΰ₯¬, 06.
|
xN
|
Like xn , but as a toggled flag, which endures until the end of the string or until the next appearance of xN in the string.
| |
xr
|
Format the next number as a roman numeral. Only works for numbers up to 10,000 (upto 3,000 in pre MediaWiki 1.20). |
{{#time:xrY}} β MMXXIV
|
xh
|
Format the next number as a Hebrew numeral. | {{#time:xhY}} β Χ'Χ"Χ
|
This parser function takes a date and/or time (in the Gregorian calendar) and formats it according to the syntax given. A date/time object can be specified; the default is the value of the magic word {{CURRENTTIMESTAMP}}
– that is, the time the page was last rendered into HTML.
{{#time: format string }}
{{#time: format string | date/time object }}
{{#time: format string | date/time object | language code }}
{{#time: format string | date/time object | language code | local }}
The list of accepted formatting codes is given in the table to the right. Any character in the formatting string that is not recognized is passed through unaltered; this applies also to blank spaces (the system does not need them for interpreting the codes). There are also two ways to escape characters within the formatting string:
- A backslash followed by a formatting character is interpreted as a single literal character
- Characters enclosed in double quotes are considered literal characters, and the quotes are removed.
In addition, the digraph xx
is interpreted as a single literal "x".
{{#time: Y-m-d }}
β 2024-12-03{{#time: [[Y]] m d }}
β 2024 12 03{{#time: [[Y (year)]] }}
β 2024 (24UTCamTue, 03 Dec 2024 11:18:20 +0000){{#time: [[Y "(year)"]] }}
β 2024 (year){{#time: i's" }}
β 18'20"
The date/time object
can be in any format accepted by PHP's strtotime() function. Both absolute (eg 20 December 2000
) and relative (eg +20 hours
) times are accepted.
{{#time: r|now}}
β Tue, 03 Dec 2024 11:18:20 +0000{{#time: r|+2 hours}}
β Tue, 03 Dec 2024 13:18:20 +0000{{#time: r|now + 2 hours}}
β Tue, 03 Dec 2024 13:18:20 +0000{{#time: r|20 December 2000}}
β Wed, 20 Dec 2000 00:00:00 +0000{{#time: r|December 20, 2000}}
β Wed, 20 Dec 2000 00:00:00 +0000{{#time: r|2000-12-20}}
β Wed, 20 Dec 2000 00:00:00 +0000{{#time: r|2000 December 20}}
→ Error: Invalid time.
The language code
in ISO 639-3 (?) allows the string to be displayed in the chosen language
{{#time:d F Y|1988-02-28|nl}}
β 28 februari 1988{{#time:l|now|uk}}
β Π²ΡΠ²ΡΠΎΡΠΎΠΊ{{#time:d xg Y|20 June 2010|pl}}
β 20 czerwca 2010
The local
parameter specifies if the date/time object refers to the local timezone or to UTC.
This is a boolean parameters: its value is determined by casting the value of the argument (see the official PHP documentation for details on how string are cast to boolean values).
$wgLocaltimezone
is set to UTC
, there is no difference in the output when local
is set to true
or false
See the following examples for details:
{{#time: Y F d H:i:s|now|it|0}}
β 2024 dicembre 03 11:18:20{{#time: Y F d H:i:s|now|it|1}}
β 2024 dicembre 03 11:18:20{{#time: Y F d H:i:s|+2 hours||0}}
β 2024 December 03 13:18:20{{#time: Y F d H:i:s|+2 hours||1}}
β 2024 December 03 13:18:20
{{#time:c|2019-05-16T17:05:43+02:00|it}}
β 2019-05-16T15:05:43+00:00{{#time:c|2019-05-16T17:05:43+02:00|it|0}}
β 2019-05-16T15:05:43+00:00{{#time:c|2019-05-16T17:05:43+02:00|it|true}}
β 2019-05-16T15:05:43+00:00
If you've calculated a Unix timestamp, you may use it in date calculations by pre-pending an @
symbol.
{{#time: U | now }}
β 1733224700{{#time: r | @1733224700 }}
β Tue, 03 Dec 2024 11:18:20 +0000
Full or partial absolute dates can be specified; the function will "fill in" parts of the date that are not specified using the current values:
{{#time: Y | January 1 }}
β 2024
A four-digit number is always interpreted as a year, never as hours and minutes:[1]
{{#time: Y m d H:i:s | 1959 }}
β 1959 12 03 00:00:00
A six-digit number is interpreted as hours, minutes and seconds if possible, but otherwise as an error (not, for instance, a year and month):
{{#time: Y m d H:i:s | 195909 }}
β 2024 12 03 19:59:09 Input is treated as a time rather than a year+month code.{{#time: Y m d H:i:s | 196009 }}
β Error: Invalid time. Although 19:60:09 is not a valid time, 196009 is not interpreted as September 1960.
The function performs a certain amount of date mathematics:
{{#time: d F Y | January 0 2008 }}
β 31 December 2007{{#time: d F | January 32 }}
β Error: Invalid time.{{#time: d F | February 29 2008 }}
β 29 February{{#time: d F | February 29 2007 }}
β 01 March{{#time:Y-F|now -1 months}}
β 2024-November
The total length of the format strings of the calls of #time
is limited to 6000 characters[2].
Time Zone issue
[edit]There is a bug in this #time parser function (more specifically in PHP DateTime) that does not allow the passing-in of non-integers as relative time zone offsets. This issue does not apply when using an on-the-hour time zone, such as EDT. For example:
- {{#time:g:i A | -4 hours }} ==> 7:18 AM
However, Venezuela is on a -4.5 hours time offset from UTC, and thus using its time zone will not normally allow the correct calculation of a relative time zone offset. Here's what happens:
- {{#time:g:i A | -4.5 hours }} ==> 8:18 PM
To workaround this issue, simply convert the time into minutes or seconds, like this:
- {{#time:g:i A | -270 minutes }} ==> 6:48 AM
- {{#time:g:i A | -16200 seconds }} ==> 6:48 AM
(Tim Starling, the developer of this function, provided the exact syntax for this solution.)
#timel
[edit]This function is identical to {{#time: ... }}
, when the local
parameter is set to true
, so it always uses the local time of the wiki (as set in $wgLocaltimezone ).
Syntax of the function is:
{{#timel: format string }}
{{#timel: format string | date/time object }}
{{#timel: format string | date/time object | language code }}
$wgLocaltimezone
is set to UTC
, there is no difference in the output when local
is set to true
or false
For instance, see the following examples:
{{#time:c|now|it}}
β 2024-12-03T11:18:20+00:00{{#time:c|now|it|0}}
β 2024-12-03T11:18:20+00:00{{#time:c|now|it|1}}
β 2024-12-03T11:18:20+00:00{{#timel:c|now|it}}
β 2024-12-03T11:18:20+00:00
- β Prior to r86805 in 2011 this was not the case.
- β ExtParserFunctions.php at phabricator.wikimedia.org