class DateTime implements DateTimeInterface (View source)

Representation of date and time.

Constants

ATOM

COOKIE

ISO8601

RFC822

RFC850

RFC1036

RFC1123

RFC2822

RFC3339

RFC3339_EXTENDED

RFC7231

RSS

W3C

Methods

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

(PHP 8 >=8.3.0)

void
__wakeup()

No description

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)

No description

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
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)

No description

array
__serialize()

No description

void
__unserialize(array $data)

No description

static DateTime
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://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()

No description

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://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.

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

Return Value

DateTime

static DateTime createFromImmutable(DateTimeImmutable $object)

Since: 7.3

No description

Parameters

DateTimeImmutable $object

Return Value

DateTime

DateTime sub(DateInterval $interval)

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

Parameters

DateInterval $interval

Return Value

DateTime

Exceptions

DateInvalidOperationException

DateTimeZone|false getTimezone()

Get the TimeZone associated with the DateTime

Return Value

DateTimeZone|false

Returns a https://secure.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

Return Value

DateTime

int getOffset()

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.

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
int $minute
int $second
int $microsecond

Added since 7.1

Return Value

DateTime

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

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

Parameters

int $year
int $month
int $day

Return Value

DateTime

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
int $week
int $dayOfWeek

Return Value

DateTime

DateTime setTimestamp(int $timestamp)

Sets the date and time based on a Unix timestamp.

Parameters

int $timestamp

Return Value

DateTime

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

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

No description

Parameters

DateTimeInterface $object

Return Value

DateTime

array __serialize()

No description

Return Value

array

void __unserialize(array $data)

No description

Parameters

array $data

Return Value

void

static DateTime createFromTimestamp(int|float $timestamp)

Since: 8.4

No description

Parameters

int|float $timestamp

Return Value

DateTime

int getMicrosecond()

Since: 8.4

No description

Return Value

int

setMicrosecond(int $microsecond)

Since: 8.4

No description

Parameters

int $microsecond