Time
extends DateTime
in package
Class Time
A localized date/time package inspired by Nesbot/Carbon and CakePHP/Chronos.
Requires the intl PHP extension.
Table of Contents
- $date : string
- $locale : string
- $relativePattern : string
- Used to check time string to determine if it is relative time or not.
- $testNow : static|DateTimeInterface|null
- $timezone : DateTimeZone
- $toStringFormat : string
- Format to use when displaying datetime through __toString
- __construct() : mixed
- Time constructor.
- __get() : mixed
- 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() : mixed
- 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() : Time
- Returns a new instance with the date time values individually set.
- createFromDate() : Time
- 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() : Time
- Provides a replacement for DateTime's own createFromFormat function, that provides more flexible timeZone handling
- createFromInstance() : Time
- Takes an instance of DateTimeInterface and returns an instance of Time with it's same values.
- createFromTime() : Time
- Returns a new instance with the date set to today, and the time set to the values passed in.
- createFromTimestamp() : Time
- 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 "current" 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() : mixed
- Returns a text string that is easily readable that describes how long ago, or how long from now, a date is, like:
- instance() : Time
- 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() : Time
- Returns a new Time instance with the timezone set.
- parse() : Time
- Returns a new Time instance while parsing a datetime string.
- sameAs() : bool
- Ensures that the times are identical, taking timezone into account.
- setDay() : Time
- Sets the day of the month.
- setHour() : Time
- Sets the hour of the day (24 hour cycle)
- setMinute() : Time
- Sets the minute of the hour
- setMonth() : Time
- Sets the month of the year.
- setSecond() : Time
- Sets the second of the minute.
- setTestNow() : mixed
- Creates an instance of Time that will be returned during testing when calling 'Time::now' instead of the current time.
- setTimestamp() : Time
- Returns a new instance with the date set to the new timestamp.
- setTimezone() : Time
- Returns a new instance with the revised timezone.
- setYear() : Time
- 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() : mixed
- Returns the localized value of the date in the format 'Y-m-d H:i:s'
- today() : Time
- Return a new time with the time set to midnight.
- toFormattedDateString() : string
- Returns a localized version of the date in nicer date format:
- toLocalizedString() : string|bool
- Returns the localized value of this instance in $format.
- tomorrow() : Time
- Returns an instance set to midnight tomorrow morning.
- toTimeString() : string
- Returns a localized version of the time in nicer date format:
- yesterday() : Time
- 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() : Time
- Helper method to do the heavy lifting of the 'setX' methods.
Properties
$date
public
string
$date
$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 static|DateTimeInterface|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
Return values
mixed —__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) : mixed
See http://php.net/manual/en/language.oop5.overloading.php
Parameters
- $name : string
Return values
mixed —__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
Tags
Return values
string —__wakeup()
This is called when we unserialize the Time object.
public
__wakeup() : mixed
Return values
mixed —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 ]) : Time
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
Time —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 ]) : Time
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
Time —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 ]) : Time
Parameters
- $format : string
- $datetime : string
- $timeZone : DateTimeZone|string|null = null
Tags
Return values
Time —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 ]) : Time
Parameters
- $dateTime : DateTimeInterface
- $locale : string|null = null
Tags
Return values
Time —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 ]) : Time
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
Time —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 ]) : Time
Parameters
- $timestamp : int
- $timezone : DateTimeZone|string|null = null
- $locale : string|null = null
Tags
Return values
Time —difference()
public
difference(mixed $testTime[, string|null $timezone = null ]) : TimeDifference
Parameters
- $testTime : mixed
- $timezone : string|null = null
Tags
Return values
TimeDifference —equals()
Determines if the datetime passed in is equal to the current instance.
public
equals(Time|DateTimeInterface|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 : Time|DateTimeInterface|string
- $timezone : string|null = null
Tags
Return values
bool —getAge()
Returns the age in years from the "current" date and 'now'
public
getAge() : int
Tags
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
Return values
IntlCalendar —getDay()
Return the localized day of the month.
public
getDay() : string
Tags
Return values
string —getDayOfWeek()
Return the index of the day of the week
public
getDayOfWeek() : string
Tags
Return values
string —getDayOfYear()
Return the index of the day of the year
public
getDayOfYear() : string
Tags
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
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
Return values
string —getMonth()
Returns the localized Month
public
getMonth() : string
Tags
Return values
string —getQuarter()
Returns the number of the current quarter for the year.
public
getQuarter() : string
Tags
Return values
string —getSecond()
Return the localized seconds
public
getSecond() : string
Tags
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(mixed $time[, string|null $timezone = null ]) : DateTime|static
Parameters
- $time : mixed
- $timezone : string|null = null
Tags
Return values
DateTime|static —getWeekOfMonth()
Return the index of the week in the month
public
getWeekOfMonth() : string
Tags
Return values
string —getWeekOfYear()
Return the index of the week in the year
public
getWeekOfYear() : string
Tags
Return values
string —getYear()
Returns the localized Year
public
getYear() : string
Tags
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() : mixed
- 3 weeks ago
- in 4 days
- 6 hours ago
Tags
Return values
mixed —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 ]) : Time
Parameters
- $dateTime : DateTime
- $locale : string|null = null
Tags
Return values
Time —isAfter()
Determines if the current instance's time is after $testTime, after converting in UTC.
public
isAfter(mixed $testTime[, string|null $timezone = null ]) : bool
Parameters
- $testTime : mixed
- $timezone : string|null = null
Tags
Return values
bool —isBefore()
Determines if the current instance's time is before $testTime, after converting to UTC.
public
isBefore(mixed $testTime[, string|null $timezone = null ]) : bool
Parameters
- $testTime : mixed
- $timezone : string|null = null
Tags
Return values
bool —now()
Returns a new Time instance with the timezone set.
public
static now([string|DateTimeZone|null $timezone = null ][, string|null $locale = null ]) : Time
Parameters
- $timezone : string|DateTimeZone|null = null
- $locale : string|null = null
Tags
Return values
Time —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 ]) : Time
Example: $time = Time::parse('first day of December 2008');
Parameters
- $datetime : string
- $timezone : DateTimeZone|string|null = null
- $locale : string|null = null
Tags
Return values
Time —sameAs()
Ensures that the times are identical, taking timezone into account.
public
sameAs(Time|DateTimeInterface|string $testTime[, string|null $timezone = null ]) : bool
Parameters
- $testTime : Time|DateTimeInterface|string
- $timezone : string|null = null
Tags
Return values
bool —setDay()
Sets the day of the month.
public
setDay(int|string $value) : Time
Parameters
- $value : int|string
Tags
Return values
Time —setHour()
Sets the hour of the day (24 hour cycle)
public
setHour(int|string $value) : Time
Parameters
- $value : int|string
Tags
Return values
Time —setMinute()
Sets the minute of the hour
public
setMinute(int|string $value) : Time
Parameters
- $value : int|string
Tags
Return values
Time —setMonth()
Sets the month of the year.
public
setMonth(int|string $value) : Time
Parameters
- $value : int|string
Tags
Return values
Time —setSecond()
Sets the second of the minute.
public
setSecond(int|string $value) : Time
Parameters
- $value : int|string
Tags
Return values
Time —setTestNow()
Creates an instance of Time that will be returned during testing when calling 'Time::now' instead of the current time.
public
static setTestNow([Time|DateTimeInterface|string|null $datetime = null ][, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : mixed
Parameters
- $datetime : Time|DateTimeInterface|string|null = null
- $timezone : DateTimeZone|string|null = null
- $locale : string|null = null
Tags
Return values
mixed —setTimestamp()
Returns a new instance with the date set to the new timestamp.
public
setTimestamp(int $timestamp) : Time
Parameters
- $timestamp : int
Tags
Return values
Time —setTimezone()
Returns a new instance with the revised timezone.
public
setTimezone(string|DateTimeZone $timezone) : Time
Parameters
- $timezone : string|DateTimeZone
Tags
Return values
Time —setYear()
Sets the current year for this instance.
public
setYear(int|string $value) : Time
Parameters
- $value : int|string
Tags
Return values
Time —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
Return values
string —toDateTime()
Converts the current instance to a mutable DateTime object.
public
toDateTime() : DateTime
Tags
Return values
DateTime —toDateTimeString()
Returns the localized value of the date in the format 'Y-m-d H:i:s'
public
toDateTimeString() : mixed
Tags
Return values
mixed —today()
Return a new time with the time set to midnight.
public
static today([DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : Time
Parameters
- $timezone : DateTimeZone|string|null = null
- $locale : string|null = null
Tags
Return values
Time —toFormattedDateString()
Returns a localized version of the date in nicer date format:
public
toFormattedDateString() : string
i.e. Apr 1, 2017
Tags
Return values
string —toLocalizedString()
Returns the localized value of this instance in $format.
public
toLocalizedString([string|null $format = null ]) : string|bool
Parameters
- $format : string|null = null
Tags
Return values
string|bool —tomorrow()
Returns an instance set to midnight tomorrow morning.
public
static tomorrow([DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : Time
Parameters
- $timezone : DateTimeZone|string|null = null
- $locale : string|null = null
Tags
Return values
Time —toTimeString()
Returns a localized version of the time in nicer date format:
public
toTimeString() : string
i.e. 13:20:33
Tags
Return values
string —yesterday()
Returns an instance set to midnight yesterday morning.
public
static yesterday([DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : Time
Parameters
- $timezone : DateTimeZone|string|null = null
- $locale : string|null = null
Tags
Return values
Time —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) : Time
Parameters
- $name : string
- $value : int