PHP Luminova: Immutable Date and Time Operations
Create, compare, and format immutable date and time values. Generate calendar data and calculate durations or expiration times.
Time extends PHP’s DateTimeImmutable. Use it to parse a date in a specific timezone, convert the instant for storage or display, and calculate time intervals. Methods that change a date return a new instance. For relative dates, create a Time for the current date with Time::now(), then call modify() on the result, for example Time::now()->modify('+3 hours').
use Luminova\Time\Time;
$local = Time::parse('2026-01-15 09:00:00', 'Asia/Kuala_Lumpur');
echo $local->toUTC()->format('Y-m-d H:i:s'); // 2026-01-15 01:00:00For start, expiration, and reminder checks, see Date and Time Task Validation.
Class Definition
- Class namespace:
Luminova\Time\Time - Parent class: \DateTimeImmutable
- Class implements: \Stringable
Methods
constructor
Time constructor.
public function __construct(?string $datetime = null, DateTimeZone|string|null $timezone = null)Parameters:
| Parameter | Type | Description |
|---|---|---|
$datetime | ?string | Optional datetime string (default null). |
$timezone | DateTimeZone|string|null | Optional timezone (default null). |
Throws:
- Luminova\Exceptions\DateTimeException - Throws if error occurs during DateTimeImmutable object construction.
timezone
Resolves a timezone name or returns the supplied timezone object.
public static function timezone(DateTimeZone|string $timezone): DateTimeZoneParameters:
| Parameter | Type | Description |
|---|---|---|
$timezone | DateTimeZone|string | Optional timezone to associate with the current DateTime instance. |
Return:
DateTimeZone - Return instance of timezone.
now
Returns current DateTime instance with the timezone set.
public static function now(DateTimeZone|string|null $timezone = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$timezone | DateTimeZone|string|null | Optional timezone. |
Return:
self - New DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurs.
parse
Create a Time instance from an explicit date such as 2026-01-15 09:00:00.
public static function parse(string $datetime, DateTimeZone|string|null $timezone = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$datetime | string | Datetime string to parse. |
$timezone | DateTimeZone|string|null | Optional timezone. |
Return:
self - New DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurs.
today
Return a new time with the time set to midnight.
public static function today(DateTimeZone|string|null $timezone = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$timezone | DateTimeZone|string|null | Optional timezone. |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
yesterday
Returns an instance set to midnight yesterday morning.
public static function yesterday(DateTimeZone|string|null $timezone = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$timezone | DateTimeZone|string|null | Optional timezone. |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
setTimezone
Set the TimeZone associated with the DateTime, and returns a new instance with the updated timezone.
public function setTimezone(DateTimeZone|string $timezone): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$timezone | DateTimeZone|string | Timezone to set. |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
getYear
Gets year from the current datetime format.
public function getYear(): stringReturn:
string - Return year
getMonth
Gets month from the current datetime format.
public function getMonth(): stringReturn:
string - Return month.
getDay
Gets day from the current datetime format.
public function getDay(): stringReturn:
string - Return day of the month
getHour
Gets hour (in 24-hour format), from the current datetime format.
public function getHour(): stringReturn:
string - Return hours of the day.
getMinute
Gets minutes from the current datetime format.
public function getMinute(): stringReturn:
string - Return minutes.
getSecond
Gets seconds from the current datetime format.
public function getSecond(): stringReturn:
string - Return seconds.
getDayOfYear
Gets day of the year, from the current datetime format.
public function getDayOfYear(): stringReturn:
string - Return day of the year.
getWeekOfMonth
Gets week of the month, from the current datetime format.
public function getWeekOfMonth(): stringReturn:
string - Return week of the month.
getWeekOfYear
Gets week of the year from the current datetime format.
public function getWeekOfYear(): stringReturn:
string - Return week of the year.
getDayOfWeek
Gets week of the day from the current datetime format.
public function getDayOfWeek(): stringReturn:
string - Return day of the week.
getQuarter
Gets quarter of the year, from the current datetime format.
public function getQuarter(): stringReturn:
string - Return quarter of the year.
datetime
Return string datetime of this format 'yyyy-mm-dd H:i:s'.
public static function datetime(DateTimeZone|string|null $timezone = self::UTC_TIMEZONE): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$timezone | DateTimeZone|string|null | Optional timezone string. |
Return:
string - Returns datetime string.
isDaylight
Determine if the current time is already in daylight savings.
public function isDaylight(): boolReturn:
bool - Return true if the current time is already in daylight saving, false otherwise.
isSystemTimezone
Check whether the passed timezone is the same as the application timezone.
public function isSystemTimezone(): boolReturn:
bool - true if the passed timezone is the same as the local timezone false otherwise.
isUtc
Returns boolean whether object is in UTC.
public function isUtc(): boolReturn:
bool - Whether the timezone offset is UTC.
getTimezoneName
Returns the name of the current timezone.
public function getTimezoneName(): ?stringReturn:
string|null - Return the timezone name or null if couldn't be resolved.
getMaxAge
Calculates the maximum age in seconds until a given timestamp.
public function getMaxAge(int $timestamp): intParameters:
| Parameter | Type | Description |
|---|---|---|
$timestamp | int | The target timestamp to calculate the maximum age against.. |
Return:
int - Return the number of seconds from the current timestamp to the given timestamp.
UTC
Returns Datetime instance of UTC timezone.
public static function UTC(DateTimeInterface|string|null $datetime = null): DateTimeImmutableParameters:
| Parameter | Type | Description |
|---|---|---|
$datetime | DateTimeInterface|string|null | Immutable date-time object or explicit date string. To convert the current time, use Time::now()->toUTC(). |
Return:
DateTimeImmutable - Return a new instance normalized to UTC.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
toFormat
Returns a formatted datetime to your preferred format.
public function toFormat(?string $format = null): string|boolParameters:
| Parameter | Type | Description |
|---|---|---|
$format | ?string | Format to return (default: yyyy-MM-dd HH:mm:ss). |
Return:
string|bool - Formatted string, or false if formatting fails.
toTime
Returns a formatted time string (ex. 17:17:17).
public function toTime(): ?stringReturn:
string|null - Return time format from current object.
toDatetime
Converts the current instance to a mutable DateTime object.
public function toDatetime(): DateTimeReturn:
DateTime - Return a new mutable DateTime instance.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
fromInstance
Takes an instance of DateTimeInterface and returns an instance of Time with it's same values.
public static function fromInstance(DateTimeInterface $datetime): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$datetime | DateTimeInterface | An instance of DateTimeInterface. |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
fromTimestamp
Returns a new instance with the datetime set based on the provided UNIX timestamp.
public static function fromTimestamp(int $timestamp, DateTimeZone|string|null $timezone = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$timestamp | int | Timestamp to create datetime from. |
$timezone | DateTimeZone|string|null | Optional timezone to associate with current DateTime instance. |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
fromDate
Returns a new instance based on the year, month and day. If any of those three are left empty, will default to the current value.
public static function fromDate(
?int $year = null,
?int $month = null,
?int $day = null,
DateTimeZone|string|null $timezone = null
): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$year | ?int | Year to pass (default null). |
$month | ?int | Month to pass (default null). |
$day | ?int | Day to pass (default null). |
$timezone | DateTimeZone|string|null | Optional timezone to associate with current DateTime instance (default null). |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
fromTime
Returns a new instance with the date set to today, and the time set to the values passed in.
public static function fromTime(
?int $hour = null,
?int $minutes = null,
?int $seconds = null,
DateTimeZone|string|null $timezone = null
): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$hour | ?int | Hour to pass (default null). |
$minutes | ?int | Minutes to pass (default null). |
$seconds | ?int | Seconds to pass (default null). |
$timezone | DateTimeZone|string|null | Optional timezone to associate with current DateTime instance (default null). |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
createFrom
Returns a new instance with the date time values individually set.
public static function createFrom(
?int $year = null,
?int $month = null,
?int $day = null,
?int $hour = null,
?int $minutes = null,
?int $seconds = null,
DateTimeZone|string|null $timezone = null
): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$year | ?int | Year to pass (default null). |
$month | ?int | Month to pass (default null). |
$day | ?int | Day to pass (default null). |
$hour | ?int | Hour to pass (default null). |
$minutes | ?int | Minutes to pass (default null). |
$seconds | ?int | Seconds to pass (default null). |
$timezone | DateTimeZone|string|null | Optional timezone to associate with current DateTime instance (default null). |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
tomorrow
Returns an instance set to midnight tomorrow morning.
public static function tomorrow(DateTimeZone|string|null $timezone = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$timezone | DateTimeZone|string|null | Optional timezone to associate with current DateTime instance. |
Return:
self - Return new DateTimeImmutable object.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
fromFormat
Create new DateTimeImmutable object formatted according to the specified format.
public static function fromFormat(
string $format,
string $datetime,
DateTimeZone|string|null $timezone = null
): DateTimeImmutable|boolParameters:
| Parameter | Type | Description |
|---|---|---|
$format | string | Format to convert to |
$datetime | string | datetime to convert to format. |
$timezone | DateTimeZone|string|null | Use a DateTimeZone instance to specify a timezone (default null). |
Return:
DateTimeImmutable|bool - New instance on success, false on failure.
Throws:
- Luminova\Exceptions\DateTimeException - Throws if any error occurred.
calendar
Returns an array representation of the given calendar month.
public static function calendar(
?int $month = null,
?int $year = null,
DateTimeZone|string|null $timezone = null
): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$month | ?int | Month default is date('n). |
$year | ?int | Year default is date('y'). |
$timezone | DateTimeZone|string|null | Optional timezone to associate with current DateTime instance (default null). |
Return:
array<int,array<int,int>> - Array of weeks, each containing 7 Unix timestamps.
The array values are timestamps which allow you to easily format.
days
Get an array of dates for each day in a specific month.
public static function days(
?int $month = null,
?int $year = null,
string $format = 'd-M-Y'
): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$month | ?int | The month (1-12) (default null). |
$year | ?int | The year (e.g., 2023) (default null). |
$format | string | The format for the returned dates (default is 'd-M-Y'). |
Return:
array<int,string> - Return a list array of days.
months
Get an array of months for each month in a specific year.
public static function months(string $format = 'M', ?int $year = null): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$format | string | The format for the returned dates (default is "M"). |
$year | ?int | Four-digit year, defaults to the current year. |
Return:
array<int,string> - Return an associative array indexed by month number from 1 to 12.
years
Generate a list of years between a start and end year.
public static function years(int|string|null $start = null, int|string|null $end = null): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$start | int|string|null | Starting year. Defaults to the current year if not provided. |
$end | int|string|null | Ending year. Defaults to the current year minus 3 years if not provided. |
Return:
array<int,int> - Returns the years from $start through $end, inclusive, in the requested order.
hours
Get a list of time hours in 12-hour format with customizable intervals.
public static function hours(int $interval = 30): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$interval | int | The interval in minutes. Default is 30. |
Return:
array<int,string> - Returns times in PHP’s default timezone, formatted as "g:iA" (e.g. "12:00AM", "12:30AM").
ago
Convert datetime to relative a human-readable representation of the time elapsed since the given datetime.
public static function ago(
DateTimeImmutable|string|int $datetime,
bool $full = false,
DateTimeZone|string|null $timezone = null
): string|boolParameters:
| Parameter | Type | Description |
|---|---|---|
$datetime | DateTimeImmutable|string|int | The datetime string, Unix timestamp, or time string. |
$full | bool | Return full relative time (e.g. 1 hour, 3 minutes, 5 seconds ago) default is false. |
$timezone | DateTimeZone|string|null | Optional timezone to associate with current DateTime instance (default null). |
Return:
string|bool - Relative string on success, false if $datetime could not be resolved.
Pass Unix timestamps as integers. Use a date or time expression for string input.
passed
Check elapsed minutes from a reference time in the past.
public static function passed(
DateTimeImmutable|string|int $datetime,
int $minutes,
DateTimeZone|string|null $timezone = self::UTC_TIMEZONE
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$datetime | DateTimeImmutable|string|int | A past Unix timestamp, immutable date-time object, or date string. Pass a date-time object when using an explicit timezone. |
$minutes | int | The number of minutes to check against. |
$timezone | DateTimeZone|string|null | Optional timezone to use (default self::UTC_TIMEZONE). |
Return:
bool - Return true if the specified minutes have passed, false otherwise.
Throws:
- Luminova\Exceptions\DateTimeException - If invalid datetime was passed.
suffix
Get the suffix for a given day (e.g., 1st, 2nd, 3rd, 4th).
public static function suffix(int $day): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$day | int | The day for which to determine the suffix. |
Return:
string - Return the day with its appropriate suffix.
isRelative
Check if time string has a relative time keywords.
public static function isRelative(string $expression): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$expression | string | The datetime string to check. |
Return:
bool - Return true if the string contains relative time keywords otherwise, false.
isTimeAgo
Check if time string has a human-readable relative time keywords from ago method.
public static function isTimeAgo(string $expression): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$expression | string | The datetime string to check. |
Return:
bool - Return true if the string contains relative time keywords otherwise, false.
isAbsolute
Check if time string is a valid absolute time string.
public static function isAbsolute(string $expression): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$expression | string | The datetime string to check. |
Return:
bool - Return true if the string contains absolute time time otherwise, false.
duration
Calculate the duration between two dates or Unix timestamps.
public static function duration(
string|int $start,
string|int $end,
bool $decimal = false,
string $unit = 'seconds'
): float|intParameters:
| Parameter | Type | Description |
|---|---|---|
$start | string|int | The start date/time or Unix timestamp. |
$end | string|int | The end date/time or Unix timestamp. |
$decimal | bool | Whether to return a fractional result when applicable. Defaults to false. |
$unit | string | The unit for the returned duration. Supported values are seconds, minutes, hours, days, and weeks, including singular forms (default 'seconds'). |
Return:
float|int - The duration in the requested unit. Returns an integer when $decimal is false, otherwise a float.
Example:
echo Time::duration(0, 5400, decimal: true, unit: 'hours'); // 1.5formatDuration
Format the duration between two dates or Unix timestamps as readable text.
public static function formatDuration(
string|int $start,
string|int $end,
bool $shortName = true
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$start | string|int | The start date/time or Unix timestamp. |
$end | string|int | The end date/time or Unix timestamp. |
$shortName | bool | If true use short names (e.g, s, m, d) (default true). |
Return:
string - The formatted duration.
month
Retrieve the name of a month from its numeric value.
public static function month(int $month, bool $fullname = false): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$month | int | Month number (1 = January … 12 = December). |
$fullname | bool | If true the full name (e.g. "January"), false for the abbreviation (e.g. "Jan"). |
Return:
string - Return the name of the month by its numeric value.
toUTC
Converts the current instance to UTC.
public function toUTC(): selfReturn:
self - Return new instance with updated timezone to UTC.
toTimeAgo
Converts the current instance to relative social time ago expression.
public function toTimeAgo(bool $full = false, DateTimeZone|string|null $timezone = null): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$full | bool | When true, all non-zero time components are included (default false). |
$timezone | DateTimeZone|string|null | Optional timezone for the "now" reference point. |
Return:
string - Return social time ago format of current time object.
toString
Returns the current time formatted with Y-M-D H:i:s.
public function toString(): stringReturn:
string - Return formatted datetime string.
toTimestamp
Converts a datetime value to a Unix timestamp.
public static function toTimestamp(DateTimeInterface|string|int $datetime): intParameters:
| Parameter | Type | Description |
|---|---|---|
$datetime | DateTimeInterface|string|int | The datetime to convert to Unix timestamp. |
Return:
int - Unix timestamp (0 if invalid negative values are normalized)
isTimestamp
Validates whether the given value is a valid Unix timestamp.
public static function isTimestamp(string|int $timestamp, ?int $min = null, ?int $max = 32503680000): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$timestamp | string|int | The value to validate as a Unix timestamp. |
$min | ?int | Minimum allowed timestamp (null = no limit) (default null). |
$max | ?int | Optional maximum allowed timestamp (default: 32503680000, year 3000 boundary). |
Return:
bool - True if the value is a valid timestamp, otherwise false.
toTtl
Convert a value into TTL seconds (relative to now).
public static function toTtl(DateInterval|DateTimeInterface|string|int|null $value): ?intParameters:
| Parameter | Type | Description |
|---|---|---|
$value | DateInterval|DateTimeInterface|string|int|null | A duration in seconds, a numeric string, a date interval, a date-time object, or null. |
Return:
int|null - TTL in seconds or null if not defined.
toSeconds
Normalize datetime or interval value into seconds.
public static function toSeconds(
DateInterval|DateTimeInterface|string|int|null $value,
DateTimeZone|string|null $timezone = null
): intParameters:
| Parameter | Type | Description |
|---|---|---|
$value | DateInterval|DateTimeInterface|string|int|null | A duration in seconds, a numeric string, a date interval, a date-time object, or null. |
$timezone | DateTimeZone|string|null | Optional reference timezone (default null). |
Return:
int - Returns a duration in seconds. A date-time object yields its signed difference from now. Null and unsupported strings yield zero.
toNextCycle
Get a new Time instance advanced by the given interval cycle.
public function toNextCycle(string $cycle = 'monthly', int $unit = 1): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$cycle | string | The interval cycle to apply. Supported values: hourly, daily, weekly, monthly, quarterly, yearly, biannual, annually, decade (default 'monthly'). |
$unit | int | Number of cycle units to add (minimum: 1). |
Return:
self - Return a new Time instance with the interval added.
isAppTimezone
Returns whether the instance uses the application's configured timezone.
public function isAppTimezone(): boolReturn:
bool - True if the instance's timezone matches the configured application timezone.