Since: 5.5

class DateTimeImmutable implements DateTimeInterface (View source)

Methods

__construct(string $datetime = "now", null|DateTimeZone $timezone = null)

(PHP 8 >=8.3.0)

add(DateInterval $interval)

(PHP 5 >=5.5.0)
Adds an amount of days, months, years, hours, minutes and seconds

static DateTimeImmutable|false
createFromFormat(string $format, string $datetime, null|DateTimeZone $timezone = null)

(PHP 5 >=5.5.0)
Returns new DateTimeImmutable object formatted according to the specified format

createFromMutable(DateTime $object)

(PHP 5 >=5.6.0)
Returns new DateTimeImmutable object encapsulating the given DateTime object

static array|false
getLastErrors()

(PHP 5 >=5.5.0)
Returns the warnings and errors

modify(string $modifier)

(PHP 8 >=8.3.0)
Alters the timestamp

__set_state(array $array)

(PHP 5 >=5.5.0)
The __set_state handler

setDate(int $year, int $month, int $day)

(PHP 5 >=5.5.0)
Sets the date

setISODate(int $year, int $week, int $dayOfWeek = 1)

(PHP 5 >=5.5.0)
Sets the ISO date

setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0)

(PHP 5 >=5.5.0)
Sets the time

setTimestamp(int $timestamp)

(PHP 5 >=5.5.0)
Sets the date and time based on an Unix timestamp

setTimezone(DateTimeZone $timezone)

(PHP 5 >=5.5.0)
Sets the time zone

sub(DateInterval $interval)

(PHP 5 >=5.5.0)
Subtracts an amount of days, months, years, hours, minutes and seconds

diff(DateTimeInterface $targetObject, bool $absolute = false)

(PHP 5 >=5.5.0)
Returns the difference between two DateTime objects

string
format(string $format)

(PHP 5 >=5.5.0)
Returns date formatted according to given format

int
getOffset()

(PHP 5 >=5.5.0)
Returns the timezone offset

int
getTimestamp()

(PHP 5 >=5.5.0)
Gets the Unix timestamp

DateTimeZone|false
getTimezone()

(PHP 5 >=5.5.0)
Return time zone relative to given DateTime

void
__wakeup()

(PHP 5 >=5.5.0)
The __wakeup handler

createFromInterface(DateTimeInterface $object)

No description

array
__serialize()

No description

void
__unserialize(array $data)

No description

createFromTimestamp(int|float $timestamp)

No description

int
getMicrosecond()

No description

setMicrosecond(int $microsecond)

No description

Details

__construct(string $datetime = "now", null|DateTimeZone $timezone = null)

(PHP 8 >=8.3.0)

Parameters

string $datetime

[optional]

A date/time string. Valid formats are explained in https://secure.php.net/manual/en/datetime.formats.php Date and Time Formats.

Enter NULL here to obtain the current time when using the $timezone parameter.

null|DateTimeZone $timezone

[optional]

A https://secure.php.net/manual/en/class.datetimezone.php DateTimeZone object representing the timezone of $datetime.

If $timezone is omitted, the current timezone will be used.

Note:

The $timezone parameter and the current timezone are ignored when the $datetime parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

Exceptions

DateMalformedStringException

DateTimeImmutable add(DateInterval $interval)

(PHP 5 >=5.5.0)
Adds an amount of days, months, years, hours, minutes and seconds

Parameters

DateInterval $interval

Return Value

DateTimeImmutable

static DateTimeImmutable|false createFromFormat(string $format, string $datetime, null|DateTimeZone $timezone = null)

(PHP 5 >=5.5.0)
Returns new DateTimeImmutable object formatted according to the specified format

Parameters

string $format
string $datetime
null|DateTimeZone $timezone [optional]

Return Value

DateTimeImmutable|false

static DateTimeImmutable createFromMutable(DateTime $object)

(PHP 5 >=5.6.0)
Returns new DateTimeImmutable object encapsulating the given DateTime object

Parameters

DateTime $object

The mutable DateTime object that you want to convert to an immutable version. This object is not modified, but instead a new DateTimeImmutable object is created containing the same date time and timezone information.

Return Value

DateTimeImmutable

returns a new DateTimeImmutable instance.

static array|false getLastErrors()

(PHP 5 >=5.5.0)
Returns the warnings and errors

Return Value

array|false

Returns array containing info about warnings and errors.

DateTimeImmutable|false modify(string $modifier)

(PHP 8 >=8.3.0)
Alters the timestamp

Parameters

string $modifier

A date/time string. Valid formats are explained in https://secure.php.net/manual/en/datetime.formats.php Date and Time Formats.

Return Value

DateTimeImmutable|false

Returns the newly created object or false on failure.

Exceptions

DateMalformedStringException

static DateTimeImmutable __set_state(array $array)

(PHP 5 >=5.5.0)
The __set_state handler

Parameters

array $array

Initialization array.

Return Value

DateTimeImmutable

Returns a new instance of a https://secure.php.net/manual/en/class.datetimeimmutable.php DateTimeImmutable object.

DateTimeImmutable setDate(int $year, int $month, int $day)

(PHP 5 >=5.5.0)
Sets the date

Parameters

int $year

Year of the date.

int $month

Month of the date.

int $day

Day of the date.

Return Value

DateTimeImmutable

Returns the https://secure.php.net/manual/en/class.datetimeimmutable.php DateTimeImmutable object for method chaining or FALSE on failure.

DateTimeImmutable setISODate(int $year, int $week, int $dayOfWeek = 1)

(PHP 5 >=5.5.0)
Sets the ISO date

Parameters

int $year

Year of the date.

int $week

Week of the date.

int $dayOfWeek

[optional]

Offset from the first day of the week.

Return Value

DateTimeImmutable

Returns the https://secure.php.net/manual/en/class.datetimeimmutable.php DateTimeImmutable object for method chaining or FALSE on failure.

DateTimeImmutable setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0)

(PHP 5 >=5.5.0)
Sets the time

Parameters

int $hour

Hour of the time.

int $minute

Minute of the time.

int $second

[optional]

Second of the time.

int $microsecond

[optional]

Microseconds of the time. Added since 7.1

Return Value

DateTimeImmutable

Returns the https://secure.php.net/manual/en/class.datetimeimmutable.php DateTimeImmutable object for method chaining or FALSE on failure.

DateTimeImmutable setTimestamp(int $timestamp)

(PHP 5 >=5.5.0)
Sets the date and time based on an Unix timestamp

Parameters

int $timestamp

Unix timestamp representing the date.

Return Value

DateTimeImmutable

Returns the https://secure.php.net/manual/en/class.datetimeimmutable.php DateTimeImmutable object for method chaining or FALSE on failure.

DateTimeImmutable setTimezone(DateTimeZone $timezone)

(PHP 5 >=5.5.0)
Sets the time zone

Parameters

DateTimeZone $timezone

A https://secure.php.net/manual/en/class.datetimezone.php DateTimeZone object representing the desired time zone.

Return Value

DateTimeImmutable

Returns the https://secure.php.net/manual/en/class.datetimeimmutable.php DateTimeImmutable object for method chaining or FALSE on failure.

DateTimeImmutable sub(DateInterval $interval)

(PHP 5 >=5.5.0)
Subtracts an amount of days, months, years, hours, minutes and seconds

Parameters

DateInterval $interval

A https://secure.php.net/manual/en/class.dateinterval.php DateInterval object

Return Value

DateTimeImmutable

Exceptions

DateInvalidOperationException

DateInterval diff(DateTimeInterface $targetObject, bool $absolute = false)

(PHP 5 >=5.5.0)
Returns the difference between two DateTime objects

Parameters

DateTimeInterface $targetObject

The date to compare to.

bool $absolute

Should the interval be forced to be positive?

Return Value

DateInterval

The https://secure.php.net/manual/en/class.dateinterval.php DateInterval} object representing the difference between the two dates.

string format(string $format)

(PHP 5 >=5.5.0)
Returns date formatted according to given format

Parameters

string $format

Format accepted by https://secure.php.net/manual/en/function.date.php date().

Return Value

string

Returns the formatted date string on success or FALSE on failure. Since PHP8, it always returns STRING.

int getOffset()

(PHP 5 >=5.5.0)
Returns the timezone offset

Return Value

int

Returns the timezone offset in seconds from UTC on success or FALSE on failure. Since PHP8, it always returns INT.

int getTimestamp()

(PHP 5 >=5.5.0)
Gets the Unix timestamp

Return Value

int

Returns the Unix timestamp representing the date.

DateTimeZone|false getTimezone()

(PHP 5 >=5.5.0)
Return time zone relative to given DateTime

Return Value

DateTimeZone|false

Returns a https://secure.php.net/manual/en/class.datetimezone.php DateTimeZone object on success or FALSE on failure.

void __wakeup()

(PHP 5 >=5.5.0)
The __wakeup handler

Return Value

void

Initializes a DateTime object.

static DateTimeImmutable createFromInterface(DateTimeInterface $object)

Since: 8.0

No description

Parameters

DateTimeInterface $object

Return Value

DateTimeImmutable

array __serialize()

No description

Return Value

array

void __unserialize(array $data)

No description

Parameters

array $data

Return Value

void

static DateTimeImmutable createFromTimestamp(int|float $timestamp)

Since: 8.4

No description

Parameters

int|float $timestamp

Return Value

DateTimeImmutable

int getMicrosecond()

Since: 8.4

No description

Return Value

int

setMicrosecond(int $microsecond)

Since: 8.4

No description

Parameters

int $microsecond