class DateTime extends DateTime implements JsonSerializable (View source)

A simple DateTime wrapper with predefined formats which supports stringification and JSON.

Constants

ATOM

COOKIE

ISO8601

RFC822

RFC850

RFC1036

RFC1123

RFC2822

RFC3339

RFC3339_EXTENDED

RFC7231

RSS

W3C

Properties

string $format

The default format of any date/time type automatically converted by pq\Result (depends on the actual type of the column).

Methods

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

(PHP 8 >=8.3.0)

from  DateTime
void
__wakeup()

No description

from  DateTime
string
format(string $format)

Returns date formatted according to given format.

from  DateTime
DateTime|false
modify(string $modifier)

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

from  DateTime
add(DateInterval $interval)

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

from  DateTime
static DateTime
createFromImmutable(DateTimeImmutable $object)

No description

from  DateTime
sub(DateInterval $interval)

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

from  DateTime
DateTimeZone|false
getTimezone()

Get the TimeZone associated with the DateTime

from  DateTime
setTimezone(DateTimeZone $timezone)

Set the TimeZone associated with the DateTime

from  DateTime
int
getOffset()

Returns the timezone offset

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

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

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

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

from  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.

from  DateTime
setTimestamp(int $timestamp)

Sets the date and time based on a Unix timestamp.

from  DateTime
int
getTimestamp()

Gets the Unix timestamp.

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

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

from  DateTime
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

from  DateTime
static array|false
getLastErrors()

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

from  DateTime
static DateTime
__set_state(array $array)

The __set_state handler

from  DateTime
static DateTime
createFromInterface(DateTimeInterface $object)

No description

from  DateTime
array
__serialize()

No description

from  DateTime
void
__unserialize(array $data)

No description

from  DateTime
static DateTime
createFromTimestamp(int|float $timestamp)

No description

from  DateTime
int
getMicrosecond()

No description

from  DateTime
setMicrosecond(int $microsecond)

No description

from  DateTime
string
__toString()

Stringify the DateTime instance according to pq\DateTime::$format.

mixed
jsonSerialize()

Serialize to JSON.

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

string __toString()

Stringify the DateTime instance according to pq\DateTime::$format.

Return Value

string

the DateTime as string.

mixed jsonSerialize()

Serialize to JSON.

Alias of pq\DateTime::__toString().

Return Value

mixed

data which can be serialized by json_encode, which is a value of any type other than a resource.