Manual:wfTimestamp/zh

From mediawiki.org
This page is a translated version of the page Manual:WfTimestamp and the translation is 58% complete.

概要

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. 有关完整列表,请参阅下面的 格式

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. 改用$dbw->timestamp()

用法

wfTimestamp( $output_format, $timestamp )
  • Returns a timestamp of type string in the format specified by the $output_format argument.
  • Throws MWException if an incorrect formats is passed via the $output_format argument.
  • Returns false if an invalid or unrecognized timestamp is passed via the $timestamp argument.
参数 默认 注释
$output_format TS_UNIX Must be one of the constants listed in the formats table.
$timestamp 本级路径 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(); // 1716884004

Call with one argument to return the current time in the specified format.

echo wfTimestamp( TS_ISO_8601 ); // 2024-05-28T08:13:24Z

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 = 20240524081324; echo wfTimestamp( TS_ISO_8601, $timestamp ); // 2024-05-28T08:13:24Z $timestamp = '2024-05-28T08:13:24Z'; echo wfTimestamp( TS_RFC2822, $timestamp ); // Tue, 28 May 2024 08:13:24 GMT

格式

类型 常量(Constant) 格式[1] 示例 注释
MySQL DATETIME TS_DB Y-m-d H:i:s 2024-05-28 08:13:24
DB2 TS_DB2 Y-m-d H:i:s 2024-05-28 08:13:24 gerrit:50764版被移除
MediaWiki版本:
1.15 – 1.20
Exif TS_EXIF Y:m:d H:i:s 2024:05:28 08:13:24 Shouldn't ever be used, but is included for completeness. [2]
ISO 8601 (无时区) TS_ISO_8601 Y-m-d\TH:i:s\Z 2024-05-28T08:13:24Z Special:ExportAPI
ISO 8601 基础(无时区) TS_ISO_8601_BASIC Ymd\THis\Z 20240528T081324Z ResourceLoader 使用
MediaWiki TS_MW YmdHis 20240528081324
Oracle TS_ORACLE d-m-Y H:i:s.000000 28-05-2024 08:13:24.000000 phab:rSVN51500 之前是 'd-M-y h.i.s A' . ' +00:00'
PostgreSQL TS_POSTGRES Y-m-d H:i:s+00 2024-05-28 08:13:24+00 gerrit:459601 之前是 'Y-m-d H:i:s' . ' GMT'
RFC 2822 TS_RFC2822 D, d M Y H:i:s Tue, 28 May 2024 08:13:24 GMT 对于电子邮件和 HTTP 标头
UNIX time TS_UNIX U 1716884004 秒数(整数)
  1. Formatting codes per PHP's date() function.
  2. Documented on page 28 (for the DateTime tag) and page 36 (for the DateTimeOriginal and DateTimeDigitized tags) of the Exif 2.2 specification. 在http://exif.org/Exif2-2.PDF中下载规格

参见