Luminova Framework

PHP Luminova: Immutable Date and Time Operations

Last updated: 2026-11-28 20:57:12

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:00

For start, expiration, and reminder checks, see Date and Time Task Validation.


Class Definition


Methods

constructor

Time constructor.

public function __construct(?string $datetime = null, DateTimeZone|string|null $timezone = null)

Parameters:

ParameterTypeDescription
$datetime?stringOptional datetime string (default null).
$timezoneDateTimeZone|string|nullOptional timezone (default null).

Throws:


timezone

Resolves a timezone name or returns the supplied timezone object.

public static function timezone(DateTimeZone|string $timezone): DateTimeZone

Parameters:

ParameterTypeDescription
$timezoneDateTimeZone|stringOptional 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): self

Parameters:

ParameterTypeDescription
$timezoneDateTimeZone|string|nullOptional timezone.

Return:

self - New DateTimeImmutable object.

Throws:


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

Parameters:

ParameterTypeDescription
$datetimestringDatetime string to parse.
$timezoneDateTimeZone|string|nullOptional timezone.

Return:

self - New DateTimeImmutable object.

Throws:


today

Return a new time with the time set to midnight.

public static function today(DateTimeZone|string|null $timezone = null): self

Parameters:

ParameterTypeDescription
$timezoneDateTimeZone|string|nullOptional timezone.

Return:

self - Return new DateTimeImmutable object.

Throws:


yesterday

Returns an instance set to midnight yesterday morning.

public static function yesterday(DateTimeZone|string|null $timezone = null): self

Parameters:

ParameterTypeDescription
$timezoneDateTimeZone|string|nullOptional timezone.

Return:

self - Return new DateTimeImmutable object.

Throws:


setTimezone

Set the TimeZone associated with the DateTime, and returns a new instance with the updated timezone.

public function setTimezone(DateTimeZone|string $timezone): self

Parameters:

ParameterTypeDescription
$timezoneDateTimeZone|stringTimezone to set.

Return:

self - Return new DateTimeImmutable object.

Throws:


getYear

Gets year from the current datetime format.

public function getYear(): string

Return:

string - Return year


getMonth

Gets month from the current datetime format.

public function getMonth(): string

Return:

string - Return month.


getDay

Gets day from the current datetime format.

public function getDay(): string

Return:

string - Return day of the month


getHour

Gets hour (in 24-hour format), from the current datetime format.

public function getHour(): string

Return:

string - Return hours of the day.


getMinute

Gets minutes from the current datetime format.

public function getMinute(): string

Return:

string - Return minutes.


getSecond

Gets seconds from the current datetime format.

public function getSecond(): string

Return:

string - Return seconds.


getDayOfYear

Gets day of the year, from the current datetime format.

public function getDayOfYear(): string

Return:

string - Return day of the year.


getWeekOfMonth

Gets week of the month, from the current datetime format.

public function getWeekOfMonth(): string

Return:

string - Return week of the month.


getWeekOfYear

Gets week of the year from the current datetime format.

public function getWeekOfYear(): string

Return:

string - Return week of the year.


getDayOfWeek

Gets week of the day from the current datetime format.

public function getDayOfWeek(): string

Return:

string - Return day of the week.


getQuarter

Gets quarter of the year, from the current datetime format.

public function getQuarter(): string

Return:

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

Parameters:

ParameterTypeDescription
$timezoneDateTimeZone|string|nullOptional timezone string.

Return:

string - Returns datetime string.


isDaylight

Determine if the current time is already in daylight savings.

public function isDaylight(): bool

Return:

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(): bool

Return:

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(): bool

Return:

bool - Whether the timezone offset is UTC.


getTimezoneName

Returns the name of the current timezone.

public function getTimezoneName(): ?string

Return:

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

Parameters:

ParameterTypeDescription
$timestampintThe 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): DateTimeImmutable

Parameters:

ParameterTypeDescription
$datetimeDateTimeInterface|string|nullImmutable 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:


toFormat

Returns a formatted datetime to your preferred format.

public function toFormat(?string $format = null): string|bool

Parameters:

ParameterTypeDescription
$format?stringFormat 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(): ?string

Return:

string|null - Return time format from current object.


toDatetime

Converts the current instance to a mutable DateTime object.

public function toDatetime(): DateTime

Return:

DateTime - Return a new mutable DateTime instance.

Throws:


fromInstance

Takes an instance of DateTimeInterface and returns an instance of Time with it's same values.

public static function fromInstance(DateTimeInterface $datetime): self

Parameters:

ParameterTypeDescription
$datetimeDateTimeInterfaceAn instance of DateTimeInterface.

Return:

self - Return new DateTimeImmutable object.

Throws:


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

Parameters:

ParameterTypeDescription
$timestampintTimestamp to create datetime from.
$timezoneDateTimeZone|string|nullOptional timezone to associate with current DateTime instance.

Return:

self - Return new DateTimeImmutable object.

Throws:


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

Parameters:

ParameterTypeDescription
$year?intYear to pass (default null).
$month?intMonth to pass (default null).
$day?intDay to pass (default null).
$timezoneDateTimeZone|string|nullOptional timezone to associate with current DateTime instance (default null).

Return:

self - Return new DateTimeImmutable object.

Throws:


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

Parameters:

ParameterTypeDescription
$hour?intHour to pass (default null).
$minutes?intMinutes to pass (default null).
$seconds?intSeconds to pass (default null).
$timezoneDateTimeZone|string|nullOptional timezone to associate with current DateTime instance (default null).

Return:

self - Return new DateTimeImmutable object.

Throws:


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

Parameters:

ParameterTypeDescription
$year?intYear to pass (default null).
$month?intMonth to pass (default null).
$day?intDay to pass (default null).
$hour?intHour to pass (default null).
$minutes?intMinutes to pass (default null).
$seconds?intSeconds to pass (default null).
$timezoneDateTimeZone|string|nullOptional timezone to associate with current DateTime instance (default null).

Return:

self - Return new DateTimeImmutable object.

Throws:


tomorrow

Returns an instance set to midnight tomorrow morning.

public static function tomorrow(DateTimeZone|string|null $timezone = null): self

Parameters:

ParameterTypeDescription
$timezoneDateTimeZone|string|nullOptional timezone to associate with current DateTime instance.

Return:

self - Return new DateTimeImmutable object.

Throws:


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

Parameters:

ParameterTypeDescription
$formatstringFormat to convert to
$datetimestringdatetime to convert to format.
$timezoneDateTimeZone|string|nullUse a DateTimeZone instance to specify a timezone (default null).

Return:

DateTimeImmutable|bool - New instance on success, false on failure.

Throws:


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

Parameters:

ParameterTypeDescription
$month?intMonth default is date('n).
$year?intYear default is date('y').
$timezoneDateTimeZone|string|nullOptional 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'
): array

Parameters:

ParameterTypeDescription
$month?intThe month (1-12) (default null).
$year?intThe year (e.g., 2023) (default null).
$formatstringThe 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): array

Parameters:

ParameterTypeDescription
$formatstringThe format for the returned dates (default is "M").
$year?intFour-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): array

Parameters:

ParameterTypeDescription
$startint&#124;string&#124;nullStarting year. Defaults to the current year if not provided.
$endint&#124;string&#124;nullEnding 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): array

Parameters:

ParameterTypeDescription
$intervalintThe 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|bool

Parameters:

ParameterTypeDescription
$datetimeDateTimeImmutable&#124;string&#124;intThe datetime string, Unix timestamp, or time string.
$fullboolReturn full relative time (e.g. 1 hour, 3 minutes, 5 seconds ago) default is false.
$timezoneDateTimeZone&#124;string&#124;nullOptional 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
): bool

Parameters:

ParameterTypeDescription
$datetimeDateTimeImmutable&#124;string&#124;intA past Unix timestamp, immutable date-time object, or date string. Pass a date-time object when using an explicit timezone.
$minutesintThe number of minutes to check against.
$timezoneDateTimeZone&#124;string&#124;nullOptional timezone to use (default self::UTC_TIMEZONE).

Return:

bool - Return true if the specified minutes have passed, false otherwise.

Throws:


suffix

Get the suffix for a given day (e.g., 1st, 2nd, 3rd, 4th).

public static function suffix(int $day): string

Parameters:

ParameterTypeDescription
$dayintThe 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): bool

Parameters:

ParameterTypeDescription
$expressionstringThe 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): bool

Parameters:

ParameterTypeDescription
$expressionstringThe 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): bool

Parameters:

ParameterTypeDescription
$expressionstringThe 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|int

Parameters:

ParameterTypeDescription
$startstring&#124;intThe start date/time or Unix timestamp.
$endstring&#124;intThe end date/time or Unix timestamp.
$decimalboolWhether to return a fractional result when applicable. Defaults to false.
$unitstringThe 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.5

formatDuration

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

Parameters:

ParameterTypeDescription
$startstring&#124;intThe start date/time or Unix timestamp.
$endstring&#124;intThe end date/time or Unix timestamp.
$shortNameboolIf 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): string

Parameters:

ParameterTypeDescription
$monthintMonth number (1 = January … 12 = December).
$fullnameboolIf 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(): self

Return:

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

Parameters:

ParameterTypeDescription
$fullboolWhen true, all non-zero time components are included (default false).
$timezoneDateTimeZone&#124;string&#124;nullOptional 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(): string

Return:

string - Return formatted datetime string.


toTimestamp

Converts a datetime value to a Unix timestamp.

public static function toTimestamp(DateTimeInterface|string|int $datetime): int

Parameters:

ParameterTypeDescription
$datetimeDateTimeInterface&#124;string&#124;intThe 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): bool

Parameters:

ParameterTypeDescription
$timestampstring&#124;intThe value to validate as a Unix timestamp.
$min?intMinimum allowed timestamp (null = no limit) (default null).
$max?intOptional 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): ?int

Parameters:

ParameterTypeDescription
$valueDateInterval&#124;DateTimeInterface&#124;string&#124;int&#124;nullA 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
): int

Parameters:

ParameterTypeDescription
$valueDateInterval&#124;DateTimeInterface&#124;string&#124;int&#124;nullA duration in seconds, a numeric string, a date interval, a date-time object, or null.
$timezoneDateTimeZone&#124;string&#124;nullOptional 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): self

Parameters:

ParameterTypeDescription
$cyclestringThe interval cycle to apply. Supported values: hourly, daily, weekly, monthly, quarterly, yearly, biannual, annually, decade (default 'monthly').
$unitintNumber 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(): bool

Return:

bool - True if the instance's timezone matches the configured application timezone.