The DateTimeInterface interface
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
简介
创建 DateTimeInterface 是为了让参数、返回值、属性类型声明能够接受 DateTimeImmutable 或 DateTime 作为值。不可以使用用户定义的类实现该接口。
此接口也定义了公共常量,允许通过 DateTimeImmutable::format() 和 DateTime::format() 格式化 DateTimeImmutable 或 DateTime 对象。
接口摘要
interface DateTimeInterface {
/* 常量 */
/* 方法 */
}预定义常量
-
DateTimeInterface::ATOM
string DATE_ATOM
- Atom 格式(示例:2005-08-15T15:52:01+00:00)
DATE_COOKIE
- HTTP Cookies 格式(示例:Monday, 15-Aug-2005 15:52:01 UTC)
-
DateTimeInterface::ISO8601
string DATE_ISO8601
-
ISO-8601 格式(示例:2005-08-15T15:52:01+0000)
注意: 这种格式和 ISO-8601 格式并不兼容,只是出于向后兼容的原因才保留的。 如果要使用和 ISO-8601 兼容的格式,请使用
DateTimeInterface::ISO8601_EXPANDED
和DateTimeInterface::ATOM
两个常量。 (参见 ISO8601:2004 第 4.3.3 条 d 项) -
DateTimeInterface::ISO8601_EXPANDED
string DATE_ISO8601_EXPANDED
-
ISO-8601 扩大版(示例:+10191-07-26T08:59:52+01:00)
注意: 通过始终包含符号字符,此格式允许超出 ISO-8601 正常范围(
0000
-9999
)的年份。还确保时区部分(+01:00
)与 ISO-8601 兼容。 -
DateTimeInterface::RFC822
string DATE_RFC822
- RFC 822 格式(示例:Mon, 15 Aug 05 15:52:01 +0000)
-
DateTimeInterface::RFC850
string DATE_RFC850
- RFC 850 格式(示例:Monday, 15-Aug-05 15:52:01 UTC)
-
DateTimeInterface::RFC1036
string DATE_RFC1036
- RFC 1036(示例:Mon, 15 Aug 05 15:52:01 +0000)
-
DateTimeInterface::RFC1123
string DATE_RFC1123
- RFC 1123 格式(示例:Mon, 15 Aug 2005 15:52:01 +0000)
-
DateTimeInterface::RFC7231
string DATE_RFC7231
- RFC 7231 格式 (自 PHP 7.0.19 和 7.1.5 可用) (示例:Sat, 30 Apr 2016 17:52:13 GMT)
-
DateTimeInterface::RFC2822
string DATE_RFC2822
- RFC 2822 格式(示例:Mon, 15 Aug 2005 15:52:01 +0000)
-
DateTimeInterface::RFC3339
string DATE_RFC3339
-
同
DATE_ATOM
(自 PHP 5.1.3 版本可用) -
DateTimeInterface::RFC3339_EXTENDED
string DATE_RFC3339_EXTENDED
- RFC 3339 EXTENDED 格式(示例:2005-08-15T15:52:01.000+00:00)
-
DateTimeInterface::RSS
string DATE_RSS
- RSS(示例:Mon, 15 Aug 2005 15:52:01 +0000)
-
DateTimeInterface::W3C
string DATE_W3C
- RSS 格式(示例:2005-08-15T15:52:01+00:00)
更新日志
版本 | 说明 |
---|---|
8.4.0 | 类常量现已类型化。 |
8.2.0 |
新增常量 DateTimeInterface::ISO8601_EXPANDED 。
|
7.2.0 | DateTime 的类常量现在定义在了 DateTimeInterface 上。 |
目录
- DateTimeInterface::diff — 返回两个 DateTime 之间的差值
- DateTimeInterface::format — 按照指定格式返回格式化后的日期
- DateTimeInterface::getOffset — 返回时差
- DateTimeInterface::getTimestamp — 获取 Unix 时间戳
- DateTimeInterface::getTimezone — 返回相对于指定 DateTime 的时区
- DateTime::__wakeup — The __wakeup handler
+添加备注
用户贡献的备注 1 note
bohwaz ¶
2 years ago
Please note that if you are using DATE_RFC7231 format (used in HTTP/1.1), you'll need to change the DateTime object timezone to GMT *before*, or you'll encounter weird results, as this format DOES NOT convert the date to GMT.
So if you have a DateTime object using UTC+01:00 as its timezone, you will get a difference of 1 hour between your resulting date string and what should be the "correct" date.
Recommended use:
<?php
$date_gmt = clone $date;
$date_gmt->setTimezone(new \DateTimeZone('GMT'));
echo $date_gmt->format(DATE_RFC7231);
?>
备份地址:http://www.lvesu.com/blog/php/class.datetimeinterface.php