The DateTimeImmutable class

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

Introduction

Representation of date and time.

This class behaves the same as DateTime except new objects are returned when modification methods such as DateTime::modify() are called.

Class synopsis

classDateTimeImmutableimplementsDateTimeInterface {
publicconststringDateTimeInterface::ATOM = "Y-m-d\\TH:i:sP";
publicconststringDateTimeInterface::COOKIE = "l, d-M-Y H:i:s T";
publicconststringDateTimeInterface::ISO8601 = "Y-m-d\\TH:i:sO";
publicconststringDateTimeInterface::ISO8601_EXPANDED = "X-m-d\\TH:i:sP";
publicconststringDateTimeInterface::RFC822 = "D, d M y H:i:s O";
publicconststringDateTimeInterface::RFC850 = "l, d-M-y H:i:s T";
publicconststringDateTimeInterface::RFC1036 = "D, d M y H:i:s O";
publicconststringDateTimeInterface::RFC1123 = "D, d M Y H:i:s O";
publicconststringDateTimeInterface::RFC7231 = "D, d M Y H:i:s \\G\\M\\T";
publicconststringDateTimeInterface::RFC2822 = "D, d M Y H:i:s O";
publicconststringDateTimeInterface::RFC3339 = "Y-m-d\\TH:i:sP";
publicconststringDateTimeInterface::RFC3339_EXTENDED = "Y-m-d\\TH:i:s.vP";
publicconststringDateTimeInterface::RSS = "D, d M Y H:i:s O";
publicconststringDateTimeInterface::W3C = "Y-m-d\\TH:i:sP";
public__construct(string$datetime = "now", ?DateTimeZone$timezone = null)
publicstaticcreateFromFormat(string$format, string$datetime, ?DateTimeZone$timezone = null): DateTimeImmutable|false
publicstaticcreateFromMutable(DateTime$object): static
publicstaticgetLastErrors(): array|false
publicstatic__set_state(array$array): DateTimeImmutable
publicsetDate(int$year, int$month, int$day): DateTimeImmutable
publicsetISODate(int$year, int$week, int$dayOfWeek = 1): DateTimeImmutable
publicsetTime(
    int$hour,
    int$minute,
    int$second = 0,
    int$microsecond = 0
): DateTimeImmutable
publicdiff(DateTimeInterface$targetObject, bool$absolute = false): DateInterval
publicformat(string$format): string
publicgetOffset(): int
publicgetTimestamp(): int
public__wakeup(): void
}

Changelog

VersionDescription
7.1.0 The DateTimeImmutable constructor now includes the current microseconds in the constructed value. Before this, it would always initialise the microseconds to 0.

Table of Contents

To Top