The DateTimeInterface interface

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

简介

DateTimeInterface 的意思是可以约束类型为 DateTime 或 DateTimeImmutable。此接口不能让用户自己的 class 去实现(implements)。

类摘要

class DateTimeInterface {
/* 常量 */
const string ATOM = "Y-m-d\TH:i:sP";
const string COOKIE = "l, d-M-Y H:i:s T";
const string ISO8601 = "Y-m-d\TH:i:sO";
const string RFC822 = "D, d M y H:i:s O";
const string RFC850 = "l, d-M-y H:i:s T";
const string RFC1036 = "D, d M y H:i:s O";
const string RFC1123 = "D, d M Y H:i:s O";
const string RFC7231 = "D, d M Y H:i:s \G\M\T";
const string RFC2822 = "D, d M Y H:i:s O";
const string RFC3339 = "Y-m-d\TH:i:sP";
const string RFC3339_EXTENDED = "Y-m-d\TH:i:s.vP";
const string RSS = "D, d M Y H:i:s O";
const string W3C = "Y-m-d\TH:i:sP";
/* 方法 */
public diff(DateTimeInterface $targetObject, bool $absolute = false): DateInterval
public format(string $format): string
public getOffset(): int
public getTimestamp(): int
public getTimezone(): DateTimeZone|false
public __wakeup(): void
}

预定义常量

DateTimeInterface::ATOM
DATE_ATOM
Atom 格式(示例:2005-08-15T15:52:01+00:00)
DateTimeInterface::COOKIE
DATE_COOKIE
HTTP Cookies 格式(示例:Monday, 15-Aug-2005 15:52:01 UTC)
DateTimeInterface::ISO8601
DATE_ISO8601
ISO-8601 格式(示例:2005-08-15T15:52:01+0000)

注意: 这种格式和 ISO-8601 的格式并不兼容,只是出于向后兼容的原因才保留的。 如果要使用和 ISO-8601 兼容的格式, 请使用 DateTime::ATOMDATE_ATOM 两个常量。

DateTimeInterface::RFC822
DATE_RFC822
RFC 822 格式(示例:Mon, 15 Aug 05 15:52:01 +0000)
DateTimeInterface::RFC850
DATE_RFC850
RFC 850 格式(示例:Monday, 15-Aug-05 15:52:01 UTC)
DateTimeInterface::RFC1036
DATE_RFC1036
RFC 1036(示例:Mon, 15 Aug 05 15:52:01 +0000)
DateTimeInterface::RFC1123
DATE_RFC1123
RFC 1123 格式(示例:Mon, 15 Aug 2005 15:52:01 +0000)
DateTimeInterface::RFC7231
DATE_RFC7231
RFC 7231 格式 (自 PHP 7.0.19 和 7.1.5 可用) (示例:Sat, 30 Apr 2016 17:52:13 GMT)
DateTimeInterface::RFC2822
DATE_RFC2822
RFC 2822 格式(示例:Mon, 15 Aug 2005 15:52:01 +0000)
DateTimeInterface::RFC3339
DATE_RFC3339
DATE_ATOM(自 PHP 5.1.3 版本可用)
DateTimeInterface::RFC3339_EXTENDED
DATE_RFC3339_EXTENDED
RFC 3339 EXTENDED 格式(自 PHP 7.0.0 可用)(示例:2005-08-15T15:52:01.000+00:00)
DateTimeInterface::RSS
DATE_RSS
RSS(示例:Mon, 15 Aug 2005 15:52:01 +0000)
DateTimeInterface::W3C
DATE_W3C
RSS 格式(示例:2005-08-15T15:52:01+00:00)

更新日志

版本 说明
7.2.0 DateTime 的类常量现在定义在了 DateTimeInterface 上。
5.5.8 尝试实现(implement)DateTimeInterface 时,会抛出严重错误。 之前此举不会产生错误,但这种做法是不对的。

目录

7i24.Com