class DateTime implements DateTimeInterface (View source)

Representation of date and time.

Constants

ATOM

COOKIE

ISO8601

RFC822

RFC850

RFC1036

RFC1123

RFC2822

RFC3339

RFC3339_EXTENDED Since: 7.0

RFC7231 Since: 7.0

RSS

W3C

Methods

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

(PHP 8 >=8.3.0)

void
__wakeup()

The __wakeup handler

string
format(string $format)

Returns date formatted according to given format.

DateTime|false
modify(string $modifier)

Alter the timestamp of a DateTime object by incrementing or decrementing in a format accepted by strtotime().

add(DateInterval $interval)

Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object

static DateTime
createFromImmutable(DateTimeImmutable $object)

Returns new DateTime instance encapsulating the given DateTimeImmutable object

sub(DateInterval $interval)

Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object

DateTimeZone|false
getTimezone()

Get the TimeZone associated with the DateTime

setTimezone(DateTimeZone $timezone)

Set the TimeZone associated with the DateTime

int|false
getOffset()

Returns the timezone offset

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

Sets the current time of the DateTime object to a different time.

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

Sets the current date of the DateTime object to a different date.

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

Set a date according to the ISO 8601 standard - using weeks and day offsets rather than specific dates.

setTimestamp(int $timestamp)

Sets the date and time based on a Unix timestamp.

int
getTimestamp()

Gets the Unix timestamp.

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

Returns the difference between two DateTime objects represented as a DateInterval.

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

Parse a string into a new DateTime object according to the specified format

static array|false
getLastErrors()

Returns an array of warnings and errors found while parsing a date/time string

static DateTime
__set_state(array $array)

The __set_state handler

static DateTime
createFromInterface(DateTimeInterface $object)

Returns new DateTime object encapsulating the given DateTimeInterface object

array
__serialize()

Serialize a DateTime

void
__unserialize(array $data)

Unserialize an Datetime

static DateTime
createFromTimestamp(int|float $timestamp)

Creates an instance from a Unix timestamp

int
getMicrosecond()

Gets the microsecond part of the Unix timestamp

setMicrosecond(int $microsecond)

Sets microsecond part of the time

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://php.net/manual/en/datetime.formats.php Date and Time Formats.

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

null|DateTimeZone $timezone

[optional]

A https://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 $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

Exceptions

DateMalformedStringException

void __wakeup()

The __wakeup handler

Return Value

void

Initializes a DateTime object.

string format(string $format)

Returns date formatted according to given format.

Parameters

string $format

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

Return Value

string

Returns the formatted date string on success. Prior to PHP 8.1, FALSE was returned on failure.

DateTime|false modify(string $modifier)

Alter the timestamp of a DateTime object by incrementing or decrementing in a format accepted by strtotime().

Parameters

string $modifier

A date/time string. Valid formats are explained in Date and Time Formats.

Return Value

DateTime|false

Returns the DateTime object for method chaining or FALSE on failure.

Exceptions

DateMalformedStringException

DateTime add(DateInterval $interval)

Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object

Parameters

DateInterval $interval

A DateInterval object

Return Value

DateTime

Returns the modified DateTime object for method chaining.

static DateTime createFromImmutable(DateTimeImmutable $object)

Since: 7.3

Returns new DateTime instance encapsulating the given DateTimeImmutable object

Parameters

DateTimeImmutable $object

The immutable DateTimeImmutable object that needs to be converted to a mutable version. This object is not modified, but instead a new DateTime instance is created containing the same date, time, and timezone information.

Return Value

DateTime

Returns a new DateTime instance.

DateTime sub(DateInterval $interval)

Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object

Parameters

DateInterval $interval

A DateInterval object

Return Value

DateTime

Returns the modified DateTime object for method chaining.

Exceptions

DateInvalidOperationException

DateTimeZone|false getTimezone()

Get the TimeZone associated with the DateTime

Return Value

DateTimeZone|false

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

DateTime setTimezone(DateTimeZone $timezone)

Set the TimeZone associated with the DateTime

Parameters

DateTimeZone $timezone

A DateTimeZone object representing the desired time zone.

Return Value

DateTime

Returns the DateTime object for method chaining. The underlying point-in-time is not changed when calling this method.

int|false getOffset()

Returns the timezone offset

Return Value

int|false

Returns the timezone offset in seconds from UTC on success. Prior to PHP 8.0, FALSE was returned on failure.

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

Sets the current time of the DateTime object to a different time.

Parameters

int $hour

Hour of the time.

int $minute

Minute of the time.

int $second

Second of the time.

int $microsecond

Added since 7.1

Return Value

DateTime

Returns the modified DateTime object for method chaining.

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

Sets the current date of the DateTime object to a different date.

Parameters

int $year

Year of the date.

int $month

Month of the date.

int $day

Day of the date.

Return Value

DateTime

Returns the modified DateTime object for method chaining.

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

Set a date according to the ISO 8601 standard - using weeks and day offsets rather than specific dates.

Parameters

int $year

Year of the date.

int $week

Week of the date.

int $dayOfWeek

Offset from the first day of the week.

Return Value

DateTime

Returns the modified DateTime object for method chaining.

DateTime setTimestamp(int $timestamp)

Sets the date and time based on a Unix timestamp.

Parameters

int $timestamp

Unix timestamp representing the date. Setting timestamps outside the range of integer is possible by using DateTimeImmutable::modify with the @ format.

Return Value

DateTime

Returns the modified DateTime object for method chaining.

int getTimestamp()

Gets the Unix timestamp.

Return Value

int

Returns the Unix timestamp representing the date.

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

Returns the difference between two DateTime objects represented as a DateInterval.

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.

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

Parse a string into a new DateTime object according to the specified format

Parameters

string $format

Format accepted by date().

string $datetime

String representing the time.

null|DateTimeZone $timezone

A DateTimeZone object representing the desired time zone.

Return Value

DateTime|false

Returns a new DateTime instance or false on failure.

Exceptions

ValueError

static array|false getLastErrors()

Returns an array of warnings and errors found while parsing a date/time string

Return Value

array|false

static DateTime __set_state(array $array)

The __set_state handler

Parameters

array $array

Initialization array.

Return Value

DateTime

Returns a new instance of a DateTime object.

static DateTime createFromInterface(DateTimeInterface $object)

Since: 8.0

Returns new DateTime object encapsulating the given DateTimeInterface object

Parameters

DateTimeInterface $object

The DateTimeInterface object that needs to be converted to a mutable version. This object is not modified, but instead a new DateTime object is created containing the same date, time, and timezone information.

Return Value

DateTime

Returns a new DateTime instance.

array __serialize()

Serialize a DateTime

The __serialize() handler.

Return Value

array

The serialized representation of the DateTime object.

void __unserialize(array $data)

Unserialize an Datetime

The __unserialize() handler.

Parameters

array $data

The serialized DateTime.

Return Value

void

static DateTime createFromTimestamp(int|float $timestamp)

Since: 8.4

Creates an instance from a Unix timestamp

Parameters

int|float $timestamp

Return Value

DateTime

Exceptions

DateRangeError

int getMicrosecond()

Since: 8.4

Gets the microsecond part of the Unix timestamp

Return Value

int

DateTime setMicrosecond(int $microsecond)

Since: 8.4

Sets microsecond part of the time

Parameters

int $microsecond

Return Value

DateTime

Exceptions

DateRangeError