PHP 8.1.20 Released!

A classe DateTime

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

Introdução

Representação de data e hora.

Sinopse da classe

class DateTime implements DateTimeInterface {
/* Constantes */
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 RFC2822 = "D, d M Y H:i:s O";
const string RFC3339 = "Y-m-d\TH:i:sP";
const string RSS = "D, d M Y H:i:s O";
const string W3C = "Y-m-d\TH:i:sP";
/* Métodos */
public __construct(string $time = "now", DateTimeZone $timezone = null)
public add(DateInterval $interval): DateTime
public static createFromFormat(string $format, string $time, DateTimeZone $timezone = date_default_timezone_get()): DateTime
public static createFromImmutable(DateTimeImmutable $object): static
public static getLastErrors(): array
public modify(string $modify): DateTime
public static __set_state(array $array): DateTime
public setDate(int $year, int $month, int $day): DateTime
public setISODate(int $year, int $week, int $day = 1): DateTime
public setTime(int $hour, int $minute, int $second = 0): DateTime
public setTimestamp(int $unixtimestamp): DateTime
public setTimezone(DateTimeZone $timezone): DateTime
public sub(DateInterval $interval): DateTime
public diff(DateTimeInterface $datetime2, bool $absolute = false): DateInterval
public format(string $format): string
public getOffset(): int
public getTimestamp(): int
public __wakeup()
}

Constantes pré-definidas

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

Nota: Este formato não é compatível com a ISO-8601, mas foi deixado desta forma por razões relacionadas a retrocompatibilidade. Ao invés, use as constantes DateTime::ATOM ou DATE_ATOM para compatibilidade com a ISO-8601.

DateTime::RFC822
DATE_RFC822
RFC 822 (exemplo: Mon, 15 Aug 05 15:52:01 +0000)
DateTime::RFC850
DATE_RFC850
RFC 850 (exemplo: Monday, 15-Aug-05 15:52:01 UTC)
DateTime::RFC1036
DATE_RFC1036
RFC 1036 (exemplo: Mon, 15 Aug 05 15:52:01 +0000)
DateTime::RFC1123
DATE_RFC1123
RFC 1123 (exemplo: Mon, 15 Aug 2005 15:52:01 +0000)
DateTime::RFC2822
DATE_RFC2822
RFC 2822 (exemplo: Mon, 15 Aug 2005 15:52:01 +0000)
DateTime::RFC3339
DATE_RFC3339
Same as DATE_ATOM (since PHP 5.1.3)
DateTime::RSS
DATE_RSS
RSS (exemplo: Mon, 15 Aug 2005 15:52:01 +0000)
DateTime::W3C
DATE_W3C
World Wide Web Consortium (exemplo: 2005-08-15T15:52:01+00:00)

Changelog

Versão Descrição
5.5.0 A classe passa a implementar a interface DateTimeInterface.
5.4.24 A constante COOKIE foi modificada para refletir a RFC 1036 utilizando quatro digitos para o ano em contraposição aos dois digitos (RFC 850) em versões anteriores.
5.2.2 Comparações entre objetos com os operadores de comparação foram modificados para funcionar como esperado. Anteriomente, todos os objetos DateTime eram considerados iguais (utilizando ==).

Índice

add a note

User Contributed Notes 1 note

up
-3
szwaymi
5 months ago
A simple example to create the date time object by now with time zone

$Now = new DateTime('now', new DateTimeZone('Asia/Taipei'));

A simple example to print data time with format

echo  $Now->format('Y-m-d H:i:s');
To Top