Manual:WfTimestamp: Difference between revisions
No edit summary |
m update link |
||
| Line 113: | Line 113: | ||
<!--T:39--> |
<!--T:39--> |
||
* [<tvar name=1> |
* [<tvar name=1>https://doc.wikimedia.org/mediawiki-core/master/php/GlobalFunctions_8php.html#ae67c8f2c23d3bb2ea7173a1823355d47</tvar> source docs for wfTimestamp()] |
||
</translate> |
</translate> |
||
Revision as of 22:59, 13 June 2023
Overview
wfTimestamp() (part of GlobalFunctions.php) provides functionality to convert between common timestamp formats, including MediaWiki timestamps, UNIX timestamps, MySQL DATETIME format, RFC 2822 format and more.
See formats below for a full list.
Timestamps will be output without a timezone or in the GMT timezone, as specified by the particular format.
Never use wfTimestamp() when inserting a timestamp into the database. This will break in Postgres and possibly other non-MySQL databases. Instead use $dbw->timestamp()
Usage
wfTimestamp( $output_format, $timestamp )
- Returns a timestamp of type string in the format specified by the
$output_formatargument. - Throws
MWExceptionif an incorrect formats is passed via the$output_formatargument. - Returns
falseif an invalid or unrecognized timestamp is passed via the$timestampargument.
| Argument | Default | Notes |
|---|---|---|
$output_format
|
TS_UNIX
|
Must be one of the constants listed in the formats table. |
$timestamp
|
The current time | Should be a literal timestamp (e.g. 2010-12-03 22:07:25). Any format listed in the formats table can be used.
|
- Call with no arguments to return the current time in UNIX time format.
echo wfTimestamp(); // 1787445207
- Call with one argument to return the current time in the specified format.
echo wfTimestamp( TS_ISO_8601 ); // 2026-08-23T00:33:27Z
- Call with two arguments to return an arbitrary timestamp in the specified format.
Note that the timestamp can be in any format that wfTimestamp() can output.
$timestamp = 20260827003327;
echo wfTimestamp( TS_ISO_8601, $timestamp ); // 2026-08-23T00:33:27Z
$timestamp = '2026-08-23T00:33:27Z';
echo wfTimestamp( TS_RFC2822, $timestamp ); // Sun, 23 Aug 2026 00:33:27 GMT
Formats
| Type | Constant | Format[1] | Example | Notes | ||
|---|---|---|---|---|---|---|
MySQL DATETIME |
TS_DB |
Y-m-d H:i:s |
2026-08-23 00:33:27 | |||
| DB2 | TS_DB2 |
Y-m-d H:i:s |
2026-08-23 00:33:27 | Removed in gerrit:50764
| ||
| Exif | TS_EXIF |
Y:m:d H:i:s |
2026:08:23 00:33:27 | Shouldn't ever be used, but is included for completeness. [2] | ||
| ISO 8601 (no timezone) | TS_ISO_8601 |
Y-m-d\TH:i:s\Z |
2026-08-23T00:33:27Z | Used by Special:Export and the API | ||
| ISO 8601 basic (no timezone) | TS_ISO_8601_BASIC |
Ymd\THis\Z |
20260823T003327Z | Used by ResourceLoader | ||
| MediaWiki | TS_MW |
YmdHis |
20260823003327 | |||
| Oracle | TS_ORACLE |
d-m-Y H:i:s.000000 |
23-08-2026 00:33:27.000000 | Was 'd-M-y h.i.s A' . ' +00:00' before phab:rSVN51500 | ||
| PostgreSQL | TS_POSTGRES |
Y-m-d H:i:s+00 |
2026-08-23 00:33:27+00 | Was 'Y-m-d H:i:s' . ' GMT' before gerrit:459601 | ||
| RFC 2822 | TS_RFC2822 |
D, d M Y H:i:s |
Sun, 23 Aug 2026 00:33:27 GMT | For email and HTTP headers | ||
| UNIX time | TS_UNIX |
U |
1787445207 | Number of seconds since 1970-01-01 00:00:00 UTC |
- ↑ Formatting codes per PHP's
date()function. - ↑ Documented on page 28 (for the DateTime tag) and page 36 (for the DateTimeOriginal and DateTimeDigitized tags) of the Exif 2.2 specification. Download the specification at http://exif.org/Exif2-2.PDF