TimeTrait
This trait has properties and methods for Time and TimeLegacy.
When TimeLegacy is removed, this will be in Time.
Table of Contents
Properties
- $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
$locale
protected
string
$locale
$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
$timezone
protected
DateTimeZone
$timezone
$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
__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
staticaddHours()
Returns a new Time instance with $hours added to the time.
public
addHours(int $hours) : static
Parameters
- $hours : int
Return values
staticaddMinutes()
Returns a new Time instance with $minutes added to the time.
public
addMinutes(int $minutes) : static
Parameters
- $minutes : int
Return values
staticaddMonths()
Returns a new Time instance with $months added to the time.
public
addMonths(int $months) : static
Parameters
- $months : int
Return values
staticaddSeconds()
Returns a new Time instance with $seconds added to the time.
public
addSeconds(int $seconds) : static
Parameters
- $seconds : int
Return values
staticaddYears()
Returns a new Time instance with $years added to the time.
public
addYears(int $years) : static
Parameters
- $years : int
Return values
staticcreate()
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
Return values
selfcreateFromDate()
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
Return values
selfcreateFromFormat()
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
Attributes
- #[ReturnTypeWillChange]
Return values
selfcreateFromInstance()
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
Return values
selfcreateFromTime()
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
Return values
selfcreateFromTimestamp()
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
Return values
selfdifference()
public
difference(DateTimeInterface|self|string $testTime[, string|null $timezone = null ]) : TimeDifference
Parameters
- $testTime : DateTimeInterface|self|string
- $timezone : string|null = null
Tags
Return values
TimeDifferenceequals()
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
Return values
boolgetAge()
Returns the age in years from the date and 'now'
public
getAge() : int
Tags
Return values
intgetCalendar()
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
Return values
IntlCalendargetDay()
Return the localized day of the month.
public
getDay() : string
Tags
Return values
stringgetDayOfWeek()
Return the index of the day of the week
public
getDayOfWeek() : string
Tags
Return values
stringgetDayOfYear()
Return the index of the day of the year
public
getDayOfYear() : string
Tags
Return values
stringgetDst()
Are we in daylight savings time currently?
public
getDst() : bool
Return values
boolgetHour()
Return the localized hour (in 24-hour format).
public
getHour() : string
Tags
Return values
stringgetLocal()
Returns boolean whether the passed timezone is the same as the local timezone.
public
getLocal() : bool
Return values
boolgetMinute()
Return the localized minutes in the hour.
public
getMinute() : string
Tags
Return values
stringgetMonth()
Returns the localized Month
public
getMonth() : string
Tags
Return values
stringgetQuarter()
Returns the number of the current quarter for the year.
public
getQuarter() : string
Tags
Return values
stringgetSecond()
Return the localized seconds
public
getSecond() : string
Tags
Return values
stringgetTimezoneName()
Returns the name of the current timezone.
public
getTimezoneName() : string
Return values
stringgetUtc()
Returns boolean whether object is in UTC.
public
getUtc() : bool
Return values
boolgetUTCObject()
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
Return values
DateTime|staticgetWeekOfMonth()
Return the index of the week in the month
public
getWeekOfMonth() : string
Tags
Return values
stringgetWeekOfYear()
Return the index of the week in the year
public
getWeekOfYear() : string
Tags
Return values
stringgetYear()
Returns the localized Year
public
getYear() : string
Tags
Return values
stringhasTestNow()
Returns whether we have a testNow instance saved.
public
static hasTestNow() : bool
Return values
boolhumanize()
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
Return values
stringinstance()
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
Use createFromInstance() instead
Parameters
- $dateTime : DateTime
- $locale : string|null = null
Tags
Return values
selfisAfter()
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
Return values
boolisBefore()
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
Return values
boolnow()
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
Return values
selfparse()
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
Return values
selfsameAs()
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
Return values
boolsetDay()
Sets the day of the month.
public
setDay(int|string $value) : self
Parameters
- $value : int|string
Tags
Return values
selfsetHour()
Sets the hour of the day (24 hour cycle)
public
setHour(int|string $value) : self
Parameters
- $value : int|string
Tags
Return values
selfsetMinute()
Sets the minute of the hour
public
setMinute(int|string $value) : self
Parameters
- $value : int|string
Tags
Return values
selfsetMonth()
Sets the month of the year.
public
setMonth(int|string $value) : self
Parameters
- $value : int|string
Tags
Return values
selfsetSecond()
Sets the second of the minute.
public
setSecond(int|string $value) : self
Parameters
- $value : int|string
Tags
Return values
selfsetTestNow()
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
setTimestamp()
Returns a new instance with the date set to the new timestamp.
public
setTimestamp(int $timestamp) : self
Parameters
- $timestamp : int
Tags
Attributes
- #[ReturnTypeWillChange]
Return values
selfsetTimezone()
Returns a new instance with the revised timezone.
public
setTimezone(DateTimeZone|string $timezone) : self
Parameters
- $timezone : DateTimeZone|string
Tags
Attributes
- #[ReturnTypeWillChange]
Return values
selfsetYear()
Sets the current year for this instance.
public
setYear(int|string $value) : self
Parameters
- $value : int|string
Tags
Return values
selfsubDays()
Returns a new Time instance with $days subtracted from the time.
public
subDays(int $days) : static
Parameters
- $days : int
Return values
staticsubHours()
Returns a new Time instance with $hours subtracted from the time.
public
subHours(int $hours) : static
Parameters
- $hours : int
Return values
staticsubMinutes()
Returns a new Time instance with $minutes subtracted from the time.
public
subMinutes(int $minutes) : static
Parameters
- $minutes : int
Return values
staticsubMonths()
Returns a new Time instance with $months subtracted from the time.
public
subMonths(int $months) : static
Parameters
- $months : int
Return values
staticsubSeconds()
Returns a new Time instance with $seconds subtracted from the time.
public
subSeconds(int $seconds) : static
Parameters
- $seconds : int
Return values
staticsubYears()
Returns a new Time instance with $hours subtracted from the time.
public
subYears(int $years) : static
Parameters
- $years : int
Return values
statictoDateString()
Returns a localized version of the date in Y-m-d format.
public
toDateString() : string
Tags
Return values
stringtoDateTime()
Converts the current instance to a mutable DateTime object.
public
toDateTime() : DateTime
Tags
Return values
DateTimetoDateTimeString()
Returns the localized value of the date in the format 'Y-m-d H:i:s'
public
toDateTimeString() : false|string
Tags
Return values
false|stringtoday()
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
Return values
selftoFormattedDateString()
Returns a localized version of the date in nicer date format:
public
toFormattedDateString() : string
i.e. Apr 1, 2017
Tags
Return values
stringtoLocalizedString()
Returns the localized value of this instance in $format.
public
toLocalizedString([string|null $format = null ]) : false|string
Parameters
- $format : string|null = null
Tags
Return values
false|stringtomorrow()
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
Return values
selftoTimeString()
Returns a localized version of the time in nicer date format:
public
toTimeString() : string
i.e. 13:20:33
Tags
Return values
stringyesterday()
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
Return values
selfhasRelativeKeywords()
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
boolsetValue()
Helper method to do the heavy lifting of the 'setX' methods.
protected
setValue(string $name, int $value) : self
Parameters
- $name : string
- $value : int