A interface DateTimeInterface

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

Introdução

A interface DateTimeInterface é destinada para que DateTime e DateTimeImmutable possam fazer indução de tipo. Não é possível implementar essas interfaces em classes no espaço do usuário.

Sinopse da classe

class DateTimeInterface {
/* Métodos */
public diff(DateTimeInterface $datetime2, bool $absolute = false): DateInterval
public format(string $format): string
public getOffset(): int
public getTimestamp(): int
public __wakeup()
}

Changelog

Versão Descrição
5.5.8 Tentar implementar DateTimeInterface, lançará um erro fatal. Anteriormente, implementar a interface não lançava erro, porém, o comportamento estava incorreto.

Índice

add a note

User Contributed Notes 1 note

up
0
bohwaz
6 months 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