The DateTimeInterface interface
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
简介
DateTimeInterface 的意思是可以约束类型为 DateTime 或 DateTimeImmutable。此接口不能让用户自己的 class 去实现(implements)。
类摘要
class DateTimeInterface
{
/* 常量 */
/* 方法 */
}预定义常量
DateTimeInterface::ATOM
-
DATE_ATOM
- Atom 格式(示例:2005-08-15T15:52:01+00:00)
-
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::ATOM
或DATE_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 时,会抛出严重错误。 之前此举不会产生错误,但这种做法是不对的。 |
目录
- DateTime::diff — Returns the difference between two DateTime objects
- DateTime::format — Returns date formatted according to given format
- DateTime::getOffset — Returns the timezone offset
- DateTime::getTimestamp — Gets the Unix timestamp
- DateTime::getTimezone — Return time zone relative to given DateTime
- DateTime::__wakeup — The __wakeup handler

User Contributed Notes 1 note
divinity76 at gmail dot com ¶
1 year ago
if you want to know what all the constants looks like, try
<?php
$constants= (new ReflectionClass("DateTimeInterface"))->getConstants();
foreach($constants as $name => $format){
echo "{$name}: " . date($format, 0) . "\n";
}
?>
in php 8.0.3 it prints:
ATOM: 1970-01-01T01:00:00+01:00
COOKIE: Thursday, 01-Jan-1970 01:00:00 CET
ISO8601: 1970-01-01T01:00:00+0100
RFC822: Thu, 01 Jan 70 01:00:00 +0100
RFC850: Thursday, 01-Jan-70 01:00:00 CET
RFC1036: Thu, 01 Jan 70 01:00:00 +0100
RFC1123: Thu, 01 Jan 1970 01:00:00 +0100
RFC7231: Thu, 01 Jan 1970 01:00:00 GMT
RFC2822: Thu, 01 Jan 1970 01:00:00 +0100
RFC3339: 1970-01-01T01:00:00+01:00
RFC3339_EXTENDED: 1970-01-01T01:00:00.000+01:00
RSS: Thu, 01 Jan 1970 01:00:00 +0100
W3C: 1970-01-01T01:00:00+01:00
备份地址:http://www.lvesu.com/blog/php/class.datetimeinterface.php