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
/* Inherited constants */
/* Methods */
#[\NoDiscard(
public function add(DateInterval
message: "as DateTimeImmutable::add() does not modify the object itself")]public function add(DateInterval
$interval): DateTimeImmutablepublic static function createFromFormat(string
$format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false#[\NoDiscard(
public function modify(string
message: "as DateTimeImmutable::modify() does not modify the object itself")]public function modify(string
$modifier): DateTimeImmutable#[\NoDiscard(
public function setDate(int
message: "as DateTimeImmutable::setDate() does not modify the object itself")]public function setDate(int
$year, int $month, int $day): DateTimeImmutable#[\NoDiscard(
public function setISODate(int
message: "as DateTimeImmutable::setISODate() does not modify the object itself")]public function setISODate(int
$year, int $week, int $dayOfWeek = 1): DateTimeImmutable#[\NoDiscard(
public function setMicrosecond(int
message: "as DateTimeImmutable::setMicrosecond() does not modify the object itself")]public function setMicrosecond(int
$microsecond): static#[\NoDiscard(
public function setTime(
int
int
int
int
): DateTimeImmutable
message: "as DateTimeImmutable::setTime() does not modify the object itself")]public function setTime(
int
$hour,int
$minute,int
$second = 0,int
$microsecond = 0): DateTimeImmutable
#[\NoDiscard(
public function setTimestamp(int
message: "as DateTimeImmutable::setTimestamp() does not modify the object itself")]public function setTimestamp(int
$timestamp): DateTimeImmutable#[\NoDiscard(
public function setTimezone(DateTimeZone
message: "as DateTimeImmutable::setTimezone() does not modify the object itself")]public function setTimezone(DateTimeZone
$timezone): DateTimeImmutable#[\NoDiscard(
public function sub(DateInterval
message: "as DateTimeImmutable::sub() does not modify the object itself")]public function sub(DateInterval
$interval): DateTimeImmutable#[\Deprecated(
)]
public function __wakeup(): void
}since: '8.5',message: 'this method is obsolete, as serialization hooks are provided by __unserialize() and __serialize()',)]
public function __wakeup(): void
Changelog
| Version | Description |
|---|---|
| 8.4.0 | The class constants are now typed. |
| 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
- DateTimeImmutable::add — Returns a new object, with added amount of days, months, years, hours, minutes and seconds
- DateTimeImmutable::__construct — Returns new DateTimeImmutable object
- DateTimeImmutable::createFromFormat — Parses a time string according to a specified format
- DateTimeImmutable::createFromInterface — Returns new DateTimeImmutable object encapsulating the given DateTimeInterface object
- DateTimeImmutable::createFromMutable — Returns new DateTimeImmutable instance encapsulating the given DateTime object
- DateTimeImmutable::createFromTimestamp — Creates an instance from a Unix timestamp
- DateTimeImmutable::getLastErrors — Returns the warnings and errors
- DateTimeImmutable::modify — Creates a new object with modified timestamp
- DateTimeImmutable::__set_state — The __set_state handler
- DateTimeImmutable::setDate — Sets the date
- DateTimeImmutable::setISODate — Sets the ISO date
- DateTimeImmutable::setMicrosecond — Sets microsecond part of the time
- DateTimeImmutable::setTime — Sets the time
- DateTimeImmutable::setTimestamp — Sets the date and time based on a Unix timestamp
- DateTimeImmutable::setTimezone — Sets the time zone
- DateTimeImmutable::sub — Subtracts an amount of days, months, years, hours, minutes and seconds
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
