CodeIgniter v4.5 API

Time extends DateTimeImmutable
in package
implements Stringable uses TimeTrait

A localized date/time package inspired by Nesbot/Carbon and CakePHP/Chronos.

Requires the intl PHP extension.

Tags
see
TimeTest

Table of Contents

Interfaces

Stringable

Properties

$age  : int
$day  : string
$dayOfWeek  : string
$dayOfYear  : string
$dst  : bool
$hour  : string
$local  : bool
$minute  : string
$month  : string
$quarter  : string
$second  : string
$timestamp  : int
$utc  : bool
$weekOfMonth  : string
$weekOfYear  : string
$year  : string
$locale  : string
$relativePattern  : string
Used to check time string to determine if it is relative time or not.
$testNow  : DateTimeInterface|static|null
$timezone  : DateTimeZone
$toStringFormat  : string
Format to use when displaying datetime through __toString

Methods

__construct()  : mixed
Time constructor.
__get()  : array<string|int, mixed>|bool|DateTimeInterface|DateTimeZone|int|IntlCalendar|self|string|null
Allow for property-type access to any getX method... Note that we cannot use this for any of our setX methods, as they return new Time objects, but the __set ignores return values.
__isset()  : bool
Allow for property-type checking to any getX method.
__toString()  : string
Outputs a short format version of the datetime.
__wakeup()  : void
This is called when we unserialize the Time object.
addDays()  : static
Returns a new Time instance with $days added to the time.
addHours()  : static
Returns a new Time instance with $hours added to the time.
addMinutes()  : static
Returns a new Time instance with $minutes added to the time.
addMonths()  : static
Returns a new Time instance with $months added to the time.
addSeconds()  : static
Returns a new Time instance with $seconds added to the time.
addYears()  : static
Returns a new Time instance with $years added to the time.
create()  : self
Returns a new instance with the date time values individually set.
createFromDate()  : self
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.
createFromFormat()  : self
Provides a replacement for DateTime's own createFromFormat function, that provides more flexible timeZone handling
createFromInstance()  : self
Takes an instance of DateTimeInterface and returns an instance of Time with it's same values.
createFromTime()  : self
Returns a new instance with the date set to today, and the time set to the values passed in.
createFromTimestamp()  : self
Returns a new instance with the datetime set based on the provided UNIX timestamp.
difference()  : TimeDifference
equals()  : bool
Determines if the datetime passed in is equal to the current instance.
getAge()  : int
Returns the age in years from the date and 'now'
getCalendar()  : IntlCalendar
Returns the IntlCalendar object used for this object, taking into account the locale, date, etc.
getDay()  : string
Return the localized day of the month.
getDayOfWeek()  : string
Return the index of the day of the week
getDayOfYear()  : string
Return the index of the day of the year
getDst()  : bool
Are we in daylight savings time currently?
getHour()  : string
Return the localized hour (in 24-hour format).
getLocal()  : bool
Returns boolean whether the passed timezone is the same as the local timezone.
getMinute()  : string
Return the localized minutes in the hour.
getMonth()  : string
Returns the localized Month
getQuarter()  : string
Returns the number of the current quarter for the year.
getSecond()  : string
Return the localized seconds
getTimezoneName()  : string
Returns the name of the current timezone.
getUtc()  : bool
Returns boolean whether object is in UTC.
getUTCObject()  : DateTime|static
Returns a Time instance with the timezone converted to UTC.
getWeekOfMonth()  : string
Return the index of the week in the month
getWeekOfYear()  : string
Return the index of the week in the year
getYear()  : string
Returns the localized Year
hasTestNow()  : bool
Returns whether we have a testNow instance saved.
humanize()  : string
Returns a text string that is easily readable that describes how long ago, or how long from now, a date is, like:
instance()  : self
Takes an instance of DateTime and returns an instance of Time with it's same values.
isAfter()  : bool
Determines if the current instance's time is after $testTime, after converting in UTC.
isBefore()  : bool
Determines if the current instance's time is before $testTime, after converting to UTC.
now()  : self
Returns a new Time instance with the timezone set.
parse()  : self
Returns a new Time instance while parsing a datetime string.
sameAs()  : bool
Ensures that the times are identical, taking timezone into account.
setDay()  : self
Sets the day of the month.
setHour()  : self
Sets the hour of the day (24 hour cycle)
setMinute()  : self
Sets the minute of the hour
setMonth()  : self
Sets the month of the year.
setSecond()  : self
Sets the second of the minute.
setTestNow()  : void
Creates an instance of Time that will be returned during testing when calling 'Time::now()' instead of the current time.
setTimestamp()  : self
Returns a new instance with the date set to the new timestamp.
setTimezone()  : self
Returns a new instance with the revised timezone.
setYear()  : self
Sets the current year for this instance.
subDays()  : static
Returns a new Time instance with $days subtracted from the time.
subHours()  : static
Returns a new Time instance with $hours subtracted from the time.
subMinutes()  : static
Returns a new Time instance with $minutes subtracted from the time.
subMonths()  : static
Returns a new Time instance with $months subtracted from the time.
subSeconds()  : static
Returns a new Time instance with $seconds subtracted from the time.
subYears()  : static
Returns a new Time instance with $hours subtracted from the time.
toDateString()  : string
Returns a localized version of the date in Y-m-d format.
toDateTime()  : DateTime
Converts the current instance to a mutable DateTime object.
toDateTimeString()  : false|string
Returns the localized value of the date in the format 'Y-m-d H:i:s'
today()  : self
Return a new time with the time set to midnight.
toFormattedDateString()  : string
Returns a localized version of the date in nicer date format:
toLocalizedString()  : false|string
Returns the localized value of this instance in $format.
tomorrow()  : self
Returns an instance set to midnight tomorrow morning.
toTimeString()  : string
Returns a localized version of the time in nicer date format:
yesterday()  : self
Returns an instance set to midnight yesterday morning.
hasRelativeKeywords()  : bool
Check a time string to see if it includes a relative date (like 'next Tuesday').
setValue()  : self
Helper method to do the heavy lifting of the 'setX' methods.

Properties

$age read-only

public int $age

$day read-only

public string $day

$dayOfWeek read-only

public string $dayOfWeek

$dayOfYear read-only

public string $dayOfYear

$dst read-only

public bool $dst

$hour read-only

public string $hour

$local read-only

public bool $local

$minute read-only

public string $minute

$month read-only

public string $month

$quarter read-only

public string $quarter

$second read-only

public string $second

$timestamp read-only

public int $timestamp

$utc read-only

public bool $utc

$weekOfMonth read-only

public string $weekOfMonth

$weekOfYear read-only

public string $weekOfYear

$year read-only

public string $year

$relativePattern

Used to check time string to determine if it is relative time or not.

protected static string $relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i'

...

$testNow

protected static DateTimeInterface|static|null $testNow

$toStringFormat

Format to use when displaying datetime through __toString

protected string $toStringFormat = 'yyyy-MM-dd HH:mm:ss'

Methods

__construct()

Time constructor.

public __construct([string|null $time = null ][, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : mixed
Parameters
$time : string|null = null
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception

__get()

Allow for property-type access to any getX method... Note that we cannot use this for any of our setX methods, as they return new Time objects, but the __set ignores return values.

public __get(string $name) : array<string|int, mixed>|bool|DateTimeInterface|DateTimeZone|int|IntlCalendar|self|string|null

See http://php.net/manual/en/language.oop5.overloading.php

Parameters
$name : string
Return values
array<string|int, mixed>|bool|DateTimeInterface|DateTimeZone|int|IntlCalendar|self|string|null

__isset()

Allow for property-type checking to any getX method.

public __isset(string $name) : bool

..

Parameters
$name : string
Return values
bool

__toString()

Outputs a short format version of the datetime.

public __toString() : string

The output is NOT localized intentionally.

Return values
string

__wakeup()

This is called when we unserialize the Time object.

public __wakeup() : void

addDays()

Returns a new Time instance with $days added to the time.

public addDays(int $days) : static
Parameters
$days : int
Return values
static

addHours()

Returns a new Time instance with $hours added to the time.

public addHours(int $hours) : static
Parameters
$hours : int
Return values
static

addMinutes()

Returns a new Time instance with $minutes added to the time.

public addMinutes(int $minutes) : static
Parameters
$minutes : int
Return values
static

addMonths()

Returns a new Time instance with $months added to the time.

public addMonths(int $months) : static
Parameters
$months : int
Return values
static

addSeconds()

Returns a new Time instance with $seconds added to the time.

public addSeconds(int $seconds) : static
Parameters
$seconds : int
Return values
static

addYears()

Returns a new Time instance with $years added to the time.

public addYears(int $years) : static
Parameters
$years : int
Return values
static

create()

Returns a new instance with the date time values individually set.

public static create([int|null $year = null ][, int|null $month = null ][, int|null $day = null ][, int|null $hour = null ][, int|null $minutes = null ][, int|null $seconds = null ][, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self
Parameters
$year : int|null = null
$month : int|null = null
$day : int|null = null
$hour : int|null = null
$minutes : int|null = null
$seconds : int|null = null
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

createFromDate()

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 createFromDate([int|null $year = null ][, int|null $month = null ][, int|null $day = null ][, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self
Parameters
$year : int|null = null
$month : int|null = null
$day : int|null = null
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

createFromFormat()

Provides a replacement for DateTime's own createFromFormat function, that provides more flexible timeZone handling

public static createFromFormat(string $format, string $datetime[, DateTimeZone|string|null $timezone = null ]) : self
Parameters
$format : string
$datetime : string
$timezone : DateTimeZone|string|null = null
Tags
throws
Exception
Attributes
#[ReturnTypeWillChange]
Return values
self

createFromInstance()

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

public static createFromInstance(DateTimeInterface $dateTime[, string|null $locale = null ]) : self
Parameters
$dateTime : DateTimeInterface
$locale : string|null = null
Tags
throws
Exception
Return values
self

createFromTime()

Returns a new instance with the date set to today, and the time set to the values passed in.

public static createFromTime([int|null $hour = null ][, int|null $minutes = null ][, int|null $seconds = null ][, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self
Parameters
$hour : int|null = null
$minutes : int|null = null
$seconds : int|null = null
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

createFromTimestamp()

Returns a new instance with the datetime set based on the provided UNIX timestamp.

public static createFromTimestamp(int $timestamp[, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self
Parameters
$timestamp : int
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

difference()

public difference(DateTimeInterface|self|string $testTime[, string|null $timezone = null ]) : TimeDifference
Parameters
$testTime : DateTimeInterface|self|string
$timezone : string|null = null
Tags
throws
Exception
Return values
TimeDifference

equals()

Determines if the datetime passed in is equal to the current instance.

public equals(DateTimeInterface|self|string $testTime[, string|null $timezone = null ]) : bool

Equal in this case means that they represent the same moment in time, and are not required to be in the same timezone, as both times are converted to UTC and compared that way.

Parameters
$testTime : DateTimeInterface|self|string
$timezone : string|null = null
Tags
throws
Exception
Return values
bool

getAge()

Returns the age in years from the date and 'now'

public getAge() : int
Tags
throws
Exception
Return values
int

getCalendar()

Returns the IntlCalendar object used for this object, taking into account the locale, date, etc.

public getCalendar() : IntlCalendar

Primarily used internally to provide the difference and comparison functions, but available for public consumption if they need it.

Tags
throws
Exception
Return values
IntlCalendar

getDay()

Return the localized day of the month.

public getDay() : string
Tags
throws
Exception
Return values
string

getDayOfWeek()

Return the index of the day of the week

public getDayOfWeek() : string
Tags
throws
Exception
Return values
string

getDayOfYear()

Return the index of the day of the year

public getDayOfYear() : string
Tags
throws
Exception
Return values
string

getDst()

Are we in daylight savings time currently?

public getDst() : bool
Return values
bool

getHour()

Return the localized hour (in 24-hour format).

public getHour() : string
Tags
throws
Exception
Return values
string

getLocal()

Returns boolean whether the passed timezone is the same as the local timezone.

public getLocal() : bool
Return values
bool

getMinute()

Return the localized minutes in the hour.

public getMinute() : string
Tags
throws
Exception
Return values
string

getMonth()

Returns the localized Month

public getMonth() : string
Tags
throws
Exception
Return values
string

getQuarter()

Returns the number of the current quarter for the year.

public getQuarter() : string
Tags
throws
Exception
Return values
string

getSecond()

Return the localized seconds

public getSecond() : string
Tags
throws
Exception
Return values
string

getTimezoneName()

Returns the name of the current timezone.

public getTimezoneName() : string
Return values
string

getUtc()

Returns boolean whether object is in UTC.

public getUtc() : bool
Return values
bool

getUTCObject()

Returns a Time instance with the timezone converted to UTC.

public getUTCObject(DateTimeInterface|self|string $time[, string|null $timezone = null ]) : DateTime|static
Parameters
$time : DateTimeInterface|self|string
$timezone : string|null = null
Tags
throws
Exception
Return values
DateTime|static

getWeekOfMonth()

Return the index of the week in the month

public getWeekOfMonth() : string
Tags
throws
Exception
Return values
string

getWeekOfYear()

Return the index of the week in the year

public getWeekOfYear() : string
Tags
throws
Exception
Return values
string

getYear()

Returns the localized Year

public getYear() : string
Tags
throws
Exception
Return values
string

hasTestNow()

Returns whether we have a testNow instance saved.

public static hasTestNow() : bool
Return values
bool

humanize()

Returns a text string that is easily readable that describes how long ago, or how long from now, a date is, like:

public humanize() : string
  • 3 weeks ago
  • in 4 days
  • 6 hours ago
Tags
throws
Exception
Return values
string

instance()

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

public static instance(DateTime $dateTime[, string|null $locale = null ]) : self
Parameters
$dateTime : DateTime
$locale : string|null = null
Tags
throws
Exception
deprecated

Use createFromInstance() instead

codeCoverageIgnore
Return values
self

isAfter()

Determines if the current instance's time is after $testTime, after converting in UTC.

public isAfter(DateTimeInterface|self|string $testTime[, string|null $timezone = null ]) : bool
Parameters
$testTime : DateTimeInterface|self|string
$timezone : string|null = null
Tags
throws
Exception
Return values
bool

isBefore()

Determines if the current instance's time is before $testTime, after converting to UTC.

public isBefore(DateTimeInterface|self|string $testTime[, string|null $timezone = null ]) : bool
Parameters
$testTime : DateTimeInterface|self|string
$timezone : string|null = null
Tags
throws
Exception
Return values
bool

now()

Returns a new Time instance with the timezone set.

public static now([DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self
Parameters
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

parse()

Returns a new Time instance while parsing a datetime string.

public static parse(string $datetime[, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self

Example: $time = Time::parse('first day of December 2008');

Parameters
$datetime : string
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

sameAs()

Ensures that the times are identical, taking timezone into account.

public sameAs(DateTimeInterface|self|string $testTime[, string|null $timezone = null ]) : bool
Parameters
$testTime : DateTimeInterface|self|string
$timezone : string|null = null
Tags
throws
Exception
Return values
bool

setDay()

Sets the day of the month.

public setDay(int|string $value) : self
Parameters
$value : int|string
Tags
throws
Exception
Return values
self

setHour()

Sets the hour of the day (24 hour cycle)

public setHour(int|string $value) : self
Parameters
$value : int|string
Tags
throws
Exception
Return values
self

setMinute()

Sets the minute of the hour

public setMinute(int|string $value) : self
Parameters
$value : int|string
Tags
throws
Exception
Return values
self

setMonth()

Sets the month of the year.

public setMonth(int|string $value) : self
Parameters
$value : int|string
Tags
throws
Exception
Return values
self

setSecond()

Sets the second of the minute.

public setSecond(int|string $value) : self
Parameters
$value : int|string
Tags
throws
Exception
Return values
self

setTestNow()

Creates an instance of Time that will be returned during testing when calling 'Time::now()' instead of the current time.

public static setTestNow([DateTimeInterface|self|string|null $datetime = null ][, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : void
Parameters
$datetime : DateTimeInterface|self|string|null = null
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception

setTimestamp()

Returns a new instance with the date set to the new timestamp.

public setTimestamp(int $timestamp) : self
Parameters
$timestamp : int
Tags
throws
Exception
Attributes
#[ReturnTypeWillChange]
Return values
self

setTimezone()

Returns a new instance with the revised timezone.

public setTimezone(DateTimeZone|string $timezone) : self
Parameters
$timezone : DateTimeZone|string
Tags
throws
Exception
Attributes
#[ReturnTypeWillChange]
Return values
self

setYear()

Sets the current year for this instance.

public setYear(int|string $value) : self
Parameters
$value : int|string
Tags
throws
Exception
Return values
self

subDays()

Returns a new Time instance with $days subtracted from the time.

public subDays(int $days) : static
Parameters
$days : int
Return values
static

subHours()

Returns a new Time instance with $hours subtracted from the time.

public subHours(int $hours) : static
Parameters
$hours : int
Return values
static

subMinutes()

Returns a new Time instance with $minutes subtracted from the time.

public subMinutes(int $minutes) : static
Parameters
$minutes : int
Return values
static

subMonths()

Returns a new Time instance with $months subtracted from the time.

public subMonths(int $months) : static
Parameters
$months : int
Return values
static

subSeconds()

Returns a new Time instance with $seconds subtracted from the time.

public subSeconds(int $seconds) : static
Parameters
$seconds : int
Return values
static

subYears()

Returns a new Time instance with $hours subtracted from the time.

public subYears(int $years) : static
Parameters
$years : int
Return values
static

toDateString()

Returns a localized version of the date in Y-m-d format.

public toDateString() : string
Tags
throws
Exception
Return values
string

toDateTime()

Converts the current instance to a mutable DateTime object.

public toDateTime() : DateTime
Tags
throws
Exception
Return values
DateTime

toDateTimeString()

Returns the localized value of the date in the format 'Y-m-d H:i:s'

public toDateTimeString() : false|string
Tags
throws
Exception
Return values
false|string

today()

Return a new time with the time set to midnight.

public static today([DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self
Parameters
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

toFormattedDateString()

Returns a localized version of the date in nicer date format:

public toFormattedDateString() : string

i.e. Apr 1, 2017

Tags
throws
Exception
Return values
string

toLocalizedString()

Returns the localized value of this instance in $format.

public toLocalizedString([string|null $format = null ]) : false|string
Parameters
$format : string|null = null
Tags
throws
Exception
Return values
false|string

tomorrow()

Returns an instance set to midnight tomorrow morning.

public static tomorrow([DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self
Parameters
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

toTimeString()

Returns a localized version of the time in nicer date format:

public toTimeString() : string

i.e. 13:20:33

Tags
throws
Exception
Return values
string

yesterday()

Returns an instance set to midnight yesterday morning.

public static yesterday([DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : self
Parameters
$timezone : DateTimeZone|string|null = null
$locale : string|null = null
Tags
throws
Exception
Return values
self

hasRelativeKeywords()

Check a time string to see if it includes a relative date (like 'next Tuesday').

protected static hasRelativeKeywords(string $time) : bool
Parameters
$time : string
Return values
bool

setValue()

Helper method to do the heavy lifting of the 'setX' methods.

protected setValue(string $name, int $value) : self
Parameters
$name : string
$value : int
Tags
throws
Exception
Return values
self

        
On this page

Search results