DateTime
extends DateTimeImmutable
in package
implements
JsonSerializable, Stringable, IComparable, IEquatable
Represents a date and time
Table of Contents
Interfaces
- JsonSerializable
- Stringable
- IComparable
- Provides an interface for comparison between objects. Allows for order/sorting instances.
- IEquatable
- Provides an interface for determining the value-equality of two objects.
Constants
- FRIDAY = 5
- Represents Friday for getDayOfWeek.
- MONDAY = 1
- Represents Monday for getDayOfWeek.
- SATURDAY = 6
- Represents Saturday for getDayOfWeek.
- SUNDAY = 0
- Represents Sunday for getDayOfWeek.
- THURSDAY = 4
- Represents Thursday for getDayOfWeek.
- TUESDAY = 2
- Represents Tuesday for getDayOfWeek.
- WEDNESDAY = 3
- Represents Wednesday for getDayOfWeek.
Methods
- __construct() : DateTime
- Creates a new DateTime object.
- __toString() : string
- Creates a string representation of this object.
- add() : DateTime
- Adds a period of time to this date time object.
- addDays() : DateTime
- Adds a number of days to this instance.
- addHours() : DateTime
- Adds a number of hours to this instance.
- addMicroseconds() : DateTime
- Adds a number of microseconds to this instance.
- addMilliseconds() : DateTime
- Adds a number of milliseconds to this instance.
- addMinutes() : DateTime
- Adds a number of minutes to this instance.
- addMonths() : DateTime
- Adds a number of months to this instance.
- addSeconds() : DateTime
- Adds a number of seconds to this instance.
- addYears() : DateTime
- Adds a number of years to this instance.
- cast() : DateTime
- Makes sure a DateTimeInterface implementing object is a DateTime instance.
- compare() : int
- Compares the current instance with another and returns an integer that indicates whether the current object comes before or after or the same position and the other object.
- create() : DateTime
- Creates a new DateTime instance with the given components.
- createFromFormat() : DateTime|false
- Parses a time string and creates a DateTime using it.
- createFromInterface() : DateTime
- Creates a new DateTime instance from an interface implementation.
- createFromMutable() : DateTime
- Creates a new immutable DateTime instance from a mutable instance.
- difference() : TimeSpan
- Subtracts another Date/Time from this DateTime.
- equals() : bool
- Checks whether the current object is equal to another.
- fromUnixTimeMilliseconds() : DateTime
- Converts Unix time milliseconds to a DateTime object.
- fromUnixTimeSeconds() : DateTime
- Converts Unix time seconds to a DateTime object.
- getDate() : DateTime
- Gets the date component of this instance.
- getDay() : int
- Gets the day component of this instance.
- getDayOfWeek() : int
- Gets the day of the week represented by this instance.
- getDayOfYear() : int
- Gets the day of the year represented by this instance.
- getHour() : int
- Gets the hour component of this instance.
- getMicrosecond() : int
- Gets the microsecond component of this instance.
- getMillisecond() : int
- Gets the millisecond component of this instance.
- getMinute() : int
- Gets the minute component of this instance.
- getMonth() : int
- Gets the month component of this instance.
- getSecond() : int
- Gets the second component of this instance.
- getTimeOfDay() : TimeSpan
- Gets the time of day for this instance.
- getTimezone() : TimeZoneInfo
- Gets the time zone for this instance.
- getUnixTimeMilliseconds() : float
- Gets the number of milliseconds that have elapsed since midnight January 1st, 1970 UTC for this instance.
- getUnixTimeSeconds() : int
- Gets the number of seconds that have elapsed since midnight January 1st, 1970 UTC for this instance.
- getWeek() : int
- Gets the ISO8601 week number of this instance.
- getYear() : int
- Gets the year component of this instance.
- isDaylightSavingTime() : bool
- Gets whether Daylight Savings is in effect during the time this instance represents.
- isLeapYear() : bool
- Gets whether the year this instance represents is a leap year.
- isLessThan() : bool
- isLessThanOrEqual() : bool
- isMoreThan() : bool
- isMoreThanOrEqual() : bool
- isUTC() : bool
- Gets whether this DateTime uses UTC date/time.
- jsonSerialize() : mixed
- Returns the data which should be serialized as json.
- modify() : DateTime
- Applies a modifier on this instance and returns the result as a new instance.
- now() : DateTime
- Creates a DateTime object that is set to the current date and time, expressed in the provided time zone.
- setDate() : DateTime
- Creates a new DateTime with the given date component.
- setISODate() : DateTime
- Creates a new DateTime with the given ISO date for the date component.
- setTime() : DateTime
- Creates a new DateTime with the given time component.
- setTimestamp() : DateTime
- Creates a new DateTime with the date and time components based on a Unix timestamp.
- setTimezone() : DateTime
- Creates a new DateTime with the given time zone.
- subtract() : DateTime
- Subtracts a time period from this DateTime.
- toCookieString() : string
- Formats this DateTime as a Cookie date/time string.
- toISO8601String() : string
- Formats this DateTime as an ISO8601 date/time string.
- toNative() : DateTime
- Converts this immutable \Index\DateTime type to a mutable \DateTime type.
- toRFC822String() : string
- Formats this DateTime as an RFC 822 date/time string.
- utcNow() : DateTime
- Creates a DateTime object that is set to the current date and time, expressed in UTC.
Constants
FRIDAY
Represents Friday for getDayOfWeek.
public
int
FRIDAY
= 5
MONDAY
Represents Monday for getDayOfWeek.
public
int
MONDAY
= 1
SATURDAY
Represents Saturday for getDayOfWeek.
public
int
SATURDAY
= 6
SUNDAY
Represents Sunday for getDayOfWeek.
public
int
SUNDAY
= 0
THURSDAY
Represents Thursday for getDayOfWeek.
public
int
THURSDAY
= 4
TUESDAY
Represents Tuesday for getDayOfWeek.
public
int
TUESDAY
= 2
WEDNESDAY
Represents Wednesday for getDayOfWeek.
public
int
WEDNESDAY
= 3
Methods
__construct()
Creates a new DateTime object.
public
__construct([string $dateTime = 'now' ][, DateTimeZone|null $timeZone = null ]) : DateTime
Parameters
- $dateTime : string = 'now'
-
A date/time string.
- $timeZone : DateTimeZone|null = null
-
An object representing the desired time zone.
Tags
Return values
DateTime —A new DateTime instance.
__toString()
Creates a string representation of this object.
public
__toString() : string
Return values
string —Representation of this object.
add()
Adds a period of time to this date time object.
public
add(DateInterval $timeSpan) : DateTime
Parameters
- $timeSpan : DateInterval
-
Time period to add to this DateTime.
Return values
DateTime —A DateTime instance which is the sum of this instance and the provided period.
addDays()
Adds a number of days to this instance.
public
addDays(float $days) : DateTime
Parameters
- $days : float
-
Number of days to add.
Return values
DateTime —New DateTime instance with the days added.
addHours()
Adds a number of hours to this instance.
public
addHours(float $hours) : DateTime
Parameters
- $hours : float
-
Number of hours to add.
Return values
DateTime —New DateTime instance with the hours added.
addMicroseconds()
Adds a number of microseconds to this instance.
public
addMicroseconds(float $micros) : DateTime
Parameters
- $micros : float
-
Number of microseconds to add.
Return values
DateTime —New DateTime instance with the microseconds added.
addMilliseconds()
Adds a number of milliseconds to this instance.
public
addMilliseconds(float $millis) : DateTime
Parameters
- $millis : float
-
Number of milliseconds to add.
Return values
DateTime —New DateTime instance with the milliseconds added.
addMinutes()
Adds a number of minutes to this instance.
public
addMinutes(float $minutes) : DateTime
Parameters
- $minutes : float
-
Number of minutes to add.
Return values
DateTime —New DateTime instance with the minutes added.
addMonths()
Adds a number of months to this instance.
public
addMonths(float $months) : DateTime
Parameters
- $months : float
-
Number of months to add.
Return values
DateTime —New DateTime instance with the months added.
addSeconds()
Adds a number of seconds to this instance.
public
addSeconds(float $seconds) : DateTime
Parameters
- $seconds : float
-
Number of seconds to add.
Return values
DateTime —New DateTime instance with the seconds added.
addYears()
Adds a number of years to this instance.
public
addYears(float $years) : DateTime
Parameters
- $years : float
-
Number of years to add.
Return values
DateTime —New DateTime instance with the years added.
cast()
Makes sure a DateTimeInterface implementing object is a DateTime instance.
public
static cast(DateTimeInterface $dateTime) : DateTime
Parameters
- $dateTime : DateTimeInterface
-
Input object.
Return values
DateTime —If the input was a DateTime, the same instance will be returned. If the input was something else, a new DateTime instance will be returned based on the input.
compare()
Compares the current instance with another and returns an integer that indicates whether the current object comes before or after or the same position and the other object.
public
compare(mixed $other) : int
Parameters
- $other : mixed
Return values
int —A value that indicates the relative order of the objects being compared. Less than zero for before, zero for same position, greater than zero for after.
create()
Creates a new DateTime instance with the given components.
public
static create(int $year[, int $month = 1 ][, int $day = 1 ][, int $hour = 0 ][, int $minute = 0 ][, int $second = 0 ][, int $micros = 0 ][, DateTimeZone|null $timeZone = null ]) : DateTime
Parameters
- $year : int
-
Desired year component.
- $month : int = 1
-
Desired month component.
- $day : int = 1
-
Desired day component.
- $hour : int = 0
-
Desired hour component.
- $minute : int = 0
-
Desired minute component.
- $second : int = 0
-
Desired second component.
- $micros : int = 0
-
Desired microsecond component.
- $timeZone : DateTimeZone|null = null
-
Desired time zone.
Return values
DateTimecreateFromFormat()
Parses a time string and creates a DateTime using it.
public
static createFromFormat(string $format, string $dateTime[, DateTimeZone|null $timeZone = null ]) : DateTime|false
Parameters
- $format : string
-
Format that the passed string should be in.
- $dateTime : string
-
String representing the time.
- $timeZone : DateTimeZone|null = null
-
Desired time zone.
Tags
Return values
DateTime|false —A DateTime instance, or false on failure.
createFromInterface()
Creates a new DateTime instance from an interface implementation.
public
static createFromInterface(DateTimeInterface $object) : DateTime
Parameters
- $object : DateTimeInterface
-
Object that should be sourced.
Return values
DateTime —A new DateTime instance.
createFromMutable()
Creates a new immutable DateTime instance from a mutable instance.
public
static createFromMutable(DateTime $object) : DateTime
Parameters
- $object : DateTime
-
Mutable object that should be sourced.
Return values
DateTime —New immutable DateTime representing the same value.
difference()
Subtracts another Date/Time from this DateTime.
public
difference(DateTimeInterface $dateTime[, bool $absolute = false ]) : TimeSpan
Parameters
- $dateTime : DateTimeInterface
-
Date/time to subtract.
- $absolute : bool = false
Return values
TimeSpan —Difference between this DateTime and the provided one.
equals()
Checks whether the current object is equal to another.
public
equals(mixed $other) : bool
Parameters
- $other : mixed
Return values
bool —true if the objects are equals, false if not.
fromUnixTimeMilliseconds()
Converts Unix time milliseconds to a DateTime object.
public
static fromUnixTimeMilliseconds(float $millis) : DateTime
Parameters
- $millis : float
-
Unix time milliseconds.
Return values
DateTime —A DateTime instance representing the Unix time.
fromUnixTimeSeconds()
Converts Unix time seconds to a DateTime object.
public
static fromUnixTimeSeconds(int $seconds) : DateTime
Parameters
- $seconds : int
-
Unix time seconds.
Return values
DateTime —A DateTime instance representing the Unix time.
getDate()
Gets the date component of this instance.
public
getDate() : DateTime
Return values
DateTime —New instance with the same date but the time set to 00:00:00.
getDay()
Gets the day component of this instance.
public
getDay() : int
Return values
int —Day component, ranging from 1 to 31.
getDayOfWeek()
Gets the day of the week represented by this instance.
public
getDayOfWeek() : int
See the SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY constants for the possible return values of this function.
Return values
int —Integer between 0 and 6 representing the day of the week.
getDayOfYear()
Gets the day of the year represented by this instance.
public
getDayOfYear() : int
Return values
int —Day of the year, ranging from 1-366.
getHour()
Gets the hour component of this instance.
public
getHour() : int
Return values
int —Hour component, ranging from 0 to 23.
getMicrosecond()
Gets the microsecond component of this instance.
public
getMicrosecond() : int
Return values
int —Microsecond component, ranging from 0 to 999999.
getMillisecond()
Gets the millisecond component of this instance.
public
getMillisecond() : int
Return values
int —Millisecond component, ranging from 0 to 999.
getMinute()
Gets the minute component of this instance.
public
getMinute() : int
Return values
int —Minute component, ranging from 0 to 59.
getMonth()
Gets the month component of this instance.
public
getMonth() : int
Return values
int —Month component, ranging from 1 to 12.
getSecond()
Gets the second component of this instance.
public
getSecond() : int
Return values
int —Second component, ranging from 0 to 59.
getTimeOfDay()
Gets the time of day for this instance.
public
getTimeOfDay() : TimeSpan
Return values
TimeSpan —A TimeSpan representing the amount of time elapsed since midnight.
getTimezone()
Gets the time zone for this instance.
public
getTimezone() : TimeZoneInfo
Return values
TimeZoneInfo —Time zone for this instance.
getUnixTimeMilliseconds()
Gets the number of milliseconds that have elapsed since midnight January 1st, 1970 UTC for this instance.
public
getUnixTimeMilliseconds() : float
Return values
float —Number of milliseconds.
getUnixTimeSeconds()
Gets the number of seconds that have elapsed since midnight January 1st, 1970 UTC for this instance.
public
getUnixTimeSeconds() : int
Return values
int —Number of seconds.
getWeek()
Gets the ISO8601 week number of this instance.
public
getWeek() : int
Return values
int —Integer representing the current week number.
getYear()
Gets the year component of this instance.
public
getYear() : int
Return values
int —Year component.
isDaylightSavingTime()
Gets whether Daylight Savings is in effect during the time this instance represents.
public
isDaylightSavingTime() : bool
Return values
bool —true if DST is in effect, false if not.
isLeapYear()
Gets whether the year this instance represents is a leap year.
public
isLeapYear() : bool
Return values
bool —true if the year is a leap year, false if not.
isLessThan()
public
isLessThan(DateTimeInterface $other) : bool
Parameters
- $other : DateTimeInterface
Return values
boolisLessThanOrEqual()
public
isLessThanOrEqual(DateTimeInterface $other) : bool
Parameters
- $other : DateTimeInterface
Return values
boolisMoreThan()
public
isMoreThan(DateTimeInterface $other) : bool
Parameters
- $other : DateTimeInterface
Return values
boolisMoreThanOrEqual()
public
isMoreThanOrEqual(DateTimeInterface $other) : bool
Parameters
- $other : DateTimeInterface
Return values
boolisUTC()
Gets whether this DateTime uses UTC date/time.
public
isUTC() : bool
Return values
bool —true if UTC, false if not.
jsonSerialize()
Returns the data which should be serialized as json.
public
jsonSerialize() : mixed
Tags
Return values
mixed —Data to be passed to json_encode.
modify()
Applies a modifier on this instance and returns the result as a new instance.
public
modify(string $modifier) : DateTime
Parameters
- $modifier : string
-
A date/time format.
Tags
Return values
DateTime —The modified date/time.
now()
Creates a DateTime object that is set to the current date and time, expressed in the provided time zone.
public
static now([DateTimeZone $timeZone = null ]) : DateTime
Parameters
- $timeZone : DateTimeZone = null
-
Desired time zone, null for the current default time zone.
Return values
DateTime —An instance representing now.
setDate()
Creates a new DateTime with the given date component.
public
setDate(int $year, int $month, int $day) : DateTime
Parameters
- $year : int
-
Desired year component.
- $month : int
-
Desired month component.
- $day : int
-
Desired day component.
Return values
DateTime —A new DateTime instance with the given date component.
setISODate()
Creates a new DateTime with the given ISO date for the date component.
public
setISODate(int $year, int $week[, int $dayOfWeek = 1 ]) : DateTime
Parameters
- $year : int
-
Year of the date.
- $week : int
-
Week of the date.
- $dayOfWeek : int = 1
-
Offset from the first of the week.
Return values
DateTime —A new DateTime instance with the given date component.
setTime()
Creates a new DateTime with the given time component.
public
setTime(int $hour, int $minute[, int $second = 0 ][, int $microsecond = 0 ]) : DateTime
Parameters
- $hour : int
-
Desired hour component.
- $minute : int
-
Desired minute component.
- $second : int = 0
-
Desired second component.
- $microsecond : int = 0
-
Desired microsecond component.
Return values
DateTime —A new DateTime instance with the given time component.
setTimestamp()
Creates a new DateTime with the date and time components based on a Unix timestamp.
public
setTimestamp(int $timestamp) : DateTime
Parameters
- $timestamp : int
-
Unix timestamp representing the date.
Return values
DateTime —A new DateTime instance with the given date and time.
setTimezone()
Creates a new DateTime with the given time zone.
public
setTimezone(DateTimeZone $timeZone) : DateTime
Parameters
- $timeZone : DateTimeZone
-
An object representing the desired time zone.
Return values
DateTime —A new DateTime with the given time zone.
subtract()
Subtracts a time period from this DateTime.
public
subtract(DateInterval $timeSpan) : DateTime
Parameters
- $timeSpan : DateInterval
-
Time period to be subtracted.
Return values
DateTime —A new DateTime instance which is this instance minus the given time period.
toCookieString()
Formats this DateTime as a Cookie date/time string.
public
toCookieString() : string
Return values
string —This object represented as a Cookie date/time string.
toISO8601String()
Formats this DateTime as an ISO8601 date/time string.
public
toISO8601String() : string
Return values
string —This object represented as an ISO8601 date/time string.
toNative()
Converts this immutable \Index\DateTime type to a mutable \DateTime type.
public
toNative() : DateTime
Return values
DateTime —A new mutable \DateTime instance.
toRFC822String()
Formats this DateTime as an RFC 822 date/time string.
public
toRFC822String() : string
Return values
string —This object represented as an RFC 822 date/time string.
utcNow()
Creates a DateTime object that is set to the current date and time, expressed in UTC.
public
static utcNow() : DateTime
Return values
DateTime —An instance representing now in UTC.