The DateTime class

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

Introduction

Representation of date and time.

This class behaves the same as DateTimeImmutable, except objects are modified itself when modification methods such as DateTime::modify() are called.

Warning

Calling methods on objects of the class DateTime will change the information encapsulated in these objects, if you want to prevent that you will have to use clone operator to create a new object. Use DateTimeImmutable instead of DateTime to obtain this recommended behaviour by default.

Class synopsis

classDateTimeimplementsDateTimeInterface {
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)
publicadd(DateInterval$interval): DateTime
publicstaticcreateFromFormat(string$format, string$datetime, ?DateTimeZone$timezone = null): DateTime|false
publicstaticcreateFromImmutable(DateTimeImmutable$object): static
publicmodify(string$modifier): DateTime|false
publicstatic__set_state(array$array): DateTime
publicsetDate(int$year, int$month, int$day): DateTime
publicsetISODate(int$year, int$week, int$dayOfWeek = 1): DateTime
publicsetTime(
    int$hour,
    int$minute,
    int$second = 0,
    int$microsecond = 0
): DateTime
publicsetTimestamp(int$timestamp): DateTime
publicsub(DateInterval$interval): DateTime
publicdiff(DateTimeInterface$targetObject, bool$absolute = false): DateInterval
publicformat(string$format): string
publicgetOffset(): int
publicgetTimestamp(): int
public__wakeup(): void
}

Changelog

VersionDescription
7.2.0 The class constants of DateTime are now defined on DateTimeInterface.
7.1.0 The DateTime 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