The DateTimeInterface interface

(PHP 5 >= 5.5.0, PHP 7)

소개

DateTimeInterface is meant so that both DateTime and DateTimeImmutable can be type hinted for. It is not possible to implement this interface with userland classes.

클래스 개요

DateTimeInterface {
/* 메소드 */
public DateInterval diff ( DateTimeInterface $datetime2 [, bool $absolute = false ] )
public string format ( string $format )
public int getOffset ( void )
public int getTimestamp ( void )
public DateTimeZone getTimezone ( void )
public __wakeup ( void )
}

변경점

버전 설명
5.5.8 Trying to implement DateTimeInterface raises a fatal error now. Formerly implementing the interface didn't raise an error, but the behavior was erroneous.

Table of Contents

add a note add a note

User Contributed Notes 1 note

up
0
bohwaz
2 years 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