PHP Conference Japan 2024

DateTimeInterface 介面

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

簡介

DateTimeInterface 的建立是為了讓參數、回傳值或屬性類型宣告可以接受 DateTimeImmutableDateTime 作為值。 使用者自定義的類別無法實作此介面。

允許透過 DateTimeImmutable::format()DateTime::format() 來格式化 DateTimeImmutableDateTime 物件的常用常數,也在此介面上定義。

介面概要

介面 DateTimeInterface {
/* 常數 */
public const 字串 ATOM = "Y-m-d\\TH:i:sP";
public const 字串 COOKIE = "l, d-M-Y H:i:s T";
public const 字串 ISO8601 = "Y-m-d\\TH:i:sO";
public const 字串 ISO8601_EXPANDED = "X-m-d\\TH:i:sP";
public const 字串 RFC822 = "D, d M y H:i:s O";
public const 字串 RFC850 = "l, d-M-y H:i:s T";
public const 字串 RFC1036 = "D, d M y H:i:s O";
public const 字串 RFC1123 = "D, d M Y H:i:s O";
public const 字串 RFC7231 = "D, d M Y H:i:s \\G\\M\\T";
public const 字串 RFC2822 = "D, d M Y H:i:s O";
public const 字串 RFC3339 = "Y-m-d\\TH:i:sP";
public const 字串 RFC3339_EXTENDED = "Y-m-d\\TH:i:s.vP";
public const 字串 RSS = "D, d M Y H:i:s O";
公開 常數 字串 W3C = "Y-m-d\\TH:i:sP";
/* 方法 */
公開 diff(DateTimeInterface $targetObject, 布林值 $absolute = false): DateInterval
公開 format(字串 $format): 字串
公開 getOffset(): 整數
公開 __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 不相容,但為了向後相容性而保留。請使用 DateTimeInterface::ISO8601_EXPANDEDDateTimeInterface::ATOM 來與 ISO-8601 相容。(參考 ISO8601:2004 第 4.3.3 節 d 項)

DateTimeInterface::ISO8601_EXPANDED 字串
DATE_ISO8601_EXPANDED
ISO-8601 Expanded (例如:+10191-07-26T08:59:52+01:00)

注意此格式允許年份範圍超出 ISO-8601 的正常範圍 0000-9999,方法是始終包含正負號。它也說明時區部分 (+01:00) 與 ISO-8601 相容。

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 相同
DateTimeInterface::RFC3339_EXTENDED 字串
DATE_RFC3339_EXTENDED
RFC 3339 擴充格式 (例如: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
全球資訊網協會 (例如:2005-08-15T15:52:01+00:00)

更新日誌

版本 說明
8.4.0 類別常數現在已設定類型。
8.2.0 新增了常數 DateTimeInterface::ISO8601_EXPANDED
7.2.0 DateTime 的類別常數現在定義在 DateTimeInterface 上。

目錄

新增註釋

使用者貢獻的註釋 1 則註釋

bohwaz
2 年前
請注意,如果您使用 DATE_RFC7231 格式(用於 HTTP/1.1),您需要*事先*將 DateTime 物件的時區更改為 GMT,否則您會遇到奇怪的結果,因為此格式*不會*將日期轉換為 GMT。

因此,如果您有一個使用 UTC+01:00 作為其時區的 DateTime 物件,您將在結果日期字串和「正確」日期之間得到 1 小時的差異。

建議用法

<?php
$date_gmt
= clone $date;
$date_gmt->setTimezone(new \DateTimeZone('GMT'));
echo
$date_gmt->format(DATE_RFC7231);
?>
To Top