CakeFest 2024: The Official CakePHP Conference

Das DateTimeInterface-Interface

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

Einführung

Das DateTimeInterface wurde eingeführt, damit Parameter-, Rückgabe- oder Eigenschaftstyp-Deklarationen entweder DateTimeImmutable oder DateTime als Wert annehmen können. Es ist nicht möglich, dieses Interface in benutzerdefinierten Klassen zu implementieren.

Gemeinsame Konstanten, die es ermöglichen, DateTimeImmutable- oder DateTime-Objekte durch DateTimeImmutable::format() und DateTime::format() zu formatieren, sind in diesem Interface ebenfalls definiert.

Interface-Übersicht

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

Vordefinierte Konstanten

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

Hinweis: Dieses Format ist nicht kompatibel zu ISO-8601, wird aber aus Gründen der Abwärtskompatibilität so belassen. Für ISO-8601-Kompatibilität sollte stattdessen DateTimeInterface::ISO8601_EXPANDED oder DateTimeInterface::ATOM verwendet werden (siehe ISO8601:2004 Abschnitt 4.3.3 Punkt d).

DateTimeInterface::ISO8601_EXPANDED
DATE_ISO8601_EXPANDED
ISO-8601 erweitert (Beispiel: +10191-07-26T08:59:52+01:00)

Hinweis: Dadurch, dass dieses Format immer ein Vorzeichen enthält, können Jahresangaben außerhalb des normalen Bereichs von ISO-8601 (0000-9999) angegeben werden. Es berücksichtigt außerdem, dass der Teil mit der Zeitzone (+01:00) mit ISO-8601 kompatibel ist.

DateTimeInterface::RFC822
DATE_RFC822
RFC 822 (Beispiel: Mon, 15 Aug 05 15:52:01 +0000)
DateTimeInterface::RFC850
DATE_RFC850
RFC 850 (Beispiel: Monday, 15-Aug-05 15:52:01 UTC)
DateTimeInterface::RFC1036
DATE_RFC1036
RFC 1036 (Beispiel: Mon, 15 Aug 05 15:52:01 +0000)
DateTimeInterface::RFC1123
DATE_RFC1123
RFC 1123 (Beispiel: Mon, 15 Aug 2005 15:52:01 +0000)
DateTimeInterface::RFC7231
DATE_RFC7231
RFC 7231 (seit PHP 7.0.19 und 7.1.5) (Beispiel: Sat, 30 Apr 2016 17:52:13 GMT)
DateTimeInterface::RFC2822
DATE_RFC2822
RFC 2822 (Beispiel: Mon, 15 Aug 2005 15:52:01 +0000)
DateTimeInterface::RFC3339
DATE_RFC3339
Genau wie DATE_ATOM
DateTimeInterface::RFC3339_EXTENDED
DATE_RFC3339_EXTENDED
RFC 3339 EXTENDED Format (Beispiel: 2005-08-15T15:52:01.000+00:00)
DateTimeInterface::RSS
DATE_RSS
RSS (Beispiel: Mon, 15 Aug 2005 15:52:01 +0000)
DateTimeInterface::W3C
DATE_W3C
World Wide Web Consortium (Beispiel: 2005-08-15T15:52:01+00:00)

Changelog

Version Beschreibung
8.2.0 Die Konstante DateTimeInterface::ISO8601_EXPANDED wurde hinzugefügt.
7.2.0 Die Klassenkonstanten von DateTime sind nun in DateTimeInterface definiert.

Inhaltsverzeichnis

add a note

User Contributed Notes 1 note

up
-2
bohwaz
1 year 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);
?>
To Top