date
Table of Contents
Classes
- DurationUnit
- Defines the canonical single-letter suffixes of a duration unit.
Functions
- addDays() : DateTimeImmutable
- Returns a copy of a date shifted by a number of days.
- durationToSeconds() : float
- Normalizes a duration expressed in any supported form into a number of seconds.
- formatDateTime() : string
- Formats a given date/time string into a specified format and timezone.
- humanizeDuration() : string
- Returns a human-readable representation of a duration, e.g. `"1d 2h 3m 4s"`.
- isDate() : bool
- Indicates if the passed-in expression is a valid date with a specific format (default 'Y-m-d').
- isFuture() : bool
- Tells whether a date is strictly in the future.
- isPast() : bool
- Tells whether a date is strictly in the past.
- isValidTimezone() : bool
- Indicates if the passed-in expression is a valid timezone.
- isWeekend() : bool
- Tells whether a date falls on a weekend (Saturday or Sunday).
- now() : string
- Returns the current date/time as a formatted string.
Functions
addDays()
Returns a copy of a date shifted by a number of days.
addDays(DateTimeInterface $date, int $days) : DateTimeImmutable
A new DateTimeImmutable is returned ; the source date is never modified. A negative
$days shifts the date backwards.
Parameters
- $date : DateTimeInterface
-
The source date.
- $days : int
-
The number of days to add (negative to subtract).
Tags
Return values
DateTimeImmutable —A new immutable date shifted by $days days.
durationToSeconds()
Normalizes a duration expressed in any supported form into a number of seconds.
durationToSeconds([int|float|string|null $duration = null ][, int $hoursPerDay = 24 ]) : float
The $duration may be:
- an int|float number of seconds (e.g.
3725), returned as-is (cast to float), - a colon string
"MM:SS"or"HH:MM:SS", - a unit string
"1.5d 3h 15m 12.5s"(any subset, any order, decimals allowed), - or
null(treated as a zero duration →0.0).
A day is worth $hoursPerDay hours, so the d unit of a unit string is
converted accordingly (a colon string never carries a day component).
Parameters
- $duration : int|float|string|null = null
-
The duration to convert.
- $hoursPerDay : int = 24
-
Hours in a day for the
dunit (default 24).
Tags
Return values
float —The total number of seconds (0.0 for an empty duration).
formatDateTime()
Formats a given date/time string into a specified format and timezone.
formatDateTime([string|null $date = null ][, string $timezone = 'UTC' ][, string|null $format = 'Y-m-d\TH:i:s.v\Z' ]) : string
This function attempts to validate and parse the input date string. If the input date is not valid according to the provided format, it defaults to the current date/time ("now"). It then applies the specified timezone and returns the formatted date string.
The $timezone parameter is used only to interpret the input date string.
Regardless of the input timezone, the resulting string is always converted to UTC
and formatted with a trailing "Z".
Parameters
- $date : string|null = null
-
The input date/time string to format. If null or invalid, "now" is used.
- $timezone : string = 'UTC'
-
The timezone identifier (e.g., 'Europe/Paris'). Defaults to 'UTC'.
- $format : string|null = 'Y-m-d\TH:i:s.v\Z'
-
The date format string compatible with DateTime::format(). Defaults to 'Y-m-d\TH:i:s.v\Z' (ISO 8601 UTC with milliseconds).
Tags
Return values
string —The formatted date/time string, or null if creation fails.
humanizeDuration()
Returns a human-readable representation of a duration, e.g. `"1d 2h 3m 4s"`.
humanizeDuration([int|float|string|null $duration = null ][, int $hoursPerDay = 24 ]) : string
The $duration may be:
- an int|float number of seconds (e.g.
3725), - a colon string
"MM:SS"or"HH:MM:SS", - a unit string
"1.5d 3h 15m 12.5s"(any subset, any order), - or
null(treated as a zero duration →"0s").
Whatever the input form, the duration is first normalized into a number of
seconds and then broken down so that every component stays within its natural
range (seconds < 60, minutes < 60, hours < $hoursPerDay). Fractional input
therefore rolls up correctly: "90:00" and "1.5h" both yield "1h 30m".
Only the seconds component may carry a fractional part (e.g. "5.5s").
Parameters
- $duration : int|float|string|null = null
-
The duration to format.
- $hoursPerDay : int = 24
-
Hours in a day for day↔hour conversion (default 24).
Tags
Return values
string —The human-readable duration (e.g. "1h 42m"), "0s" for an empty duration.
isDate()
Indicates if the passed-in expression is a valid date with a specific format (default 'Y-m-d').
isDate(string|null $date[, string $format = 'Y-m-d' ]) : bool
Parameters
- $date : string|null
-
The expression to evaluate.
- $format : string = 'Y-m-d'
-
The date format (default 'Y-m-d').
Tags
Return values
bool —Indicates if the passed-in expression is a valid timezone.
isFuture()
Tells whether a date is strictly in the future.
isFuture(DateTimeInterface $date[, DateTimeInterface|null $now = null ]) : bool
Parameters
- $date : DateTimeInterface
-
The date to test.
- $now : DateTimeInterface|null = null
-
The reference "now". Defaults to the current date/time.
Tags
Return values
bool —true if $date is strictly after $now, false otherwise.
isPast()
Tells whether a date is strictly in the past.
isPast(DateTimeInterface $date[, DateTimeInterface|null $now = null ]) : bool
Parameters
- $date : DateTimeInterface
-
The date to test.
- $now : DateTimeInterface|null = null
-
The reference "now". Defaults to the current date/time.
Tags
Return values
bool —true if $date is strictly before $now, false otherwise.
isValidTimezone()
Indicates if the passed-in expression is a valid timezone.
isValidTimezone([string|null $timezone = null ]) : bool
Note: timezone_identifiers_list() requires PHP >= 5.2
Parameters
- $timezone : string|null = null
-
The timezone expression to evaluates.
Tags
Return values
bool —Indicates if the passed-in expression is a valid timezone.
isWeekend()
Tells whether a date falls on a weekend (Saturday or Sunday).
isWeekend(DateTimeInterface $date) : bool
Parameters
- $date : DateTimeInterface
-
The date to test.
Tags
Return values
bool —true if the date is a Saturday or a Sunday, false otherwise.
now()
Returns the current date/time as a formatted string.
now([string $timezone = 'UTC' ][, string|null $format = 'Y-m-d\TH:i:s.v\Z' ]) : string
The $timezone parameter is used only to interpret the "now" value.
Regardless of the input timezone, the resulting string is always converted to UTC and formatted with a trailing "Z".
Parameters
- $timezone : string = 'UTC'
-
The timezone identifier (e.g., 'Europe/Paris'). Defaults to 'UTC'.
- $format : string|null = 'Y-m-d\TH:i:s.v\Z'
-
The date format string compatible with DateTime::format(). Defaults to 'Y-m-d\TH:i:s.v\Z' (ISO 8601 UTC with milliseconds).
Tags
Return values
string —The formatted current date/time string.