Class Time
Extends the built-in DateTime class to provide handy methods and locale-aware formatting helpers
Property Summary
-
$_formatters protected static
array
In-memory cache of date formatters
-
$_jsonEncodeFormat protected static
string|array|int
The format to use when when converting this object to json
-
$_toStringFormat protected static
string|array|int
The format to use when formatting a time using
Cake\I18n\Time::i18nFormat()
and__toString
-
$defaultLocale public static
string
The default locale to be used for displaying formatted date strings.
-
$niceFormat public static
string|array|int
The format to use when formatting a time using
Cake\I18n\Time::nice()
-
$wordAccuracy public static
array
The format to use when formatting a time using
Time::timeAgoInWords()
and the difference is less thanTime::$wordEnd
-
$wordEnd public static
string
The end of relative time telling
-
$wordFormat public static
string
The format to use when formatting a time using
Cake\I18n\Time::timeAgoInWords()
and the difference is more thanCake\I18n\Time::$wordEnd
Method Summary
-
__construct() public
{@inheritDoc}
-
__debugInfo() public
Returns the data that should be displayed when debugging this object
-
__toString() public
{@inheritDoc}
-
_formatObject() protected
Returns a translated and localized date string. Implements what IntlDateFormatter::formatObject() is in PHP 5.5+
-
diffForHumans() public
Returns the difference between this date and the provided one in a human readable format.
-
i18nFormat() public
Returns a formatted string for this time object using the preferred format and language for the specified locale.
-
isThisMonth() public
Returns true if this object represents a date within the current month
-
isThisWeek() public
Returns true if this object represents a date within the current week
-
isThisYear() public
Returns true if this object represents a date within the current year
-
isWithinNext() public
Returns true this instance will happen within the specified interval
-
jsonSerialize() public
Returns a string that should be serialized when converting this object to json
-
listTimezones() public static
Get list of timezone identifiers
-
nice() public
Returns a nicely formatted date string for this object.
-
parseDate() public static
Returns a new Time object after parsing the provided $date string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.
-
parseDateTime() public static
Returns a new Time object after parsing the provided time string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.
-
parseTime() public static
Returns a new Time object after parsing the provided $time string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.
-
resetToStringFormat() public static
Resets the format used to the default when converting an instance of this type to a string
-
setJsonEncodeFormat() public static
Sets the default format used when converting this object to json
-
setToStringFormat() public static
Sets the default format used when type converting instances of this type to string
-
timeAgoInWords() public
Returns either a relative or a formatted absolute date depending on the difference between the current time and this object.
-
toQuarter() public
Returns the quarter
-
toUnixString() public
Returns a UNIX timestamp.
-
wasWithinLast() public
Returns true this instance happened within the specified interval
Method Detail
__construct() ¶ public
__construct(mixed $time = null, mixed $tz = null)
{@inheritDoc}
Parameters
-
$time optional
-
$tz optional
__debugInfo() ¶ public
__debugInfo(): array
Returns the data that should be displayed when debugging this object
Returns
array
_formatObject() ¶ protected
_formatObject(DateTime $date, string|int|array $format, string $locale): string
Returns a translated and localized date string. Implements what IntlDateFormatter::formatObject() is in PHP 5.5+
Parameters
-
DateTime
$date Date.
-
string|int|array
$format Format.
-
string
$locale The locale name in which the date should be displayed.
Returns
string
diffForHumans() ¶ public
diffForHumans(Carbon\Carbon|null $other = null, array $options = []): string
Returns the difference between this date and the provided one in a human readable format.
See Time::timeAgoInWords()
for a full list of options that can be passed
to this method.
Parameters
-
Carbon\Carbon|null
$other optional the date to diff with
-
array
$options optional options accepted by timeAgoInWords
Returns
string
See Also
i18nFormat() ¶ public
i18nFormat(string|int|null $format = null, stringDateTimeZone|null $timezone = null, string|null $locale = null): string
Returns a formatted string for this time object using the preferred format and language for the specified locale.
It is possible to specify the desired format for the string to be displayed.
You can either pass IntlDateFormatter
constants as the first argument of this
function, or pass a full ICU date formatting string as specified in the following
resource: http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details.
Examples
$time = new Time('2014-04-20 22:10');
$time->i18nFormat(); // outputs '4/20/14, 10:10 PM' for the en-US locale
$time->i18nFormat(\IntlDateFormatter::FULL); // Use the full date and time format
$time->i18nFormat([\IntlDateFormatter::FULL, \IntlDateFormatter::SHORT]); // Use full date but short time format
$time->i18nFormat('yyyy-MM-dd HH:mm:ss'); // outputs '2014-04-20 22:10'
If you wish to control the default format to be used for this method, you can alter
the value of the static Time::$defaultLocale
variable and set it to one of the
possible formats accepted by this function.
You can read about the available IntlDateFormatter constants at http://www.php.net/manual/en/class.intldateformatter.php
If you need to display the date in a different timezone than the one being used for this Time object without altering its internal state, you can pass a timezone string or object as the second parameter.
Finally, should you need to use a different locale for displaying this time object, pass a locale string as the third parameter to this function.
Examples
$time = new Time('2014-04-20 22:10');
$time->i18nFormat(null, null, 'de-DE');
$time->i18nFormat(\IntlDateFormatter::FULL, 'Europe/Berlin', 'de-DE');
You can control the default locale to be used by setting the static variable
Time::$defaultLocale
to a valid locale string. If empty, the default will be
taken from the intl.default_locale
ini config.
Parameters
-
string|int|null
$format optional Format string.
-
stringDateTimeZone|null
$timezone optional Timezone string or DateTimeZone object in which the date will be displayed. The timezone stored for this object will not be changed.
-
string|null
$locale optional The locale name in which the date should be displayed (e.g. pt-BR)
Returns
string
isThisMonth() ¶ public
isThisMonth(): bool
Returns true if this object represents a date within the current month
Returns
bool
isThisWeek() ¶ public
isThisWeek(): bool
Returns true if this object represents a date within the current week
Returns
bool
isThisYear() ¶ public
isThisYear(): bool
Returns true if this object represents a date within the current year
Returns
bool
isWithinNext() ¶ public
isWithinNext(string|int $timeInterval): bool
Returns true this instance will happen within the specified interval
Parameters
-
string|int
$timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
Returns
bool
jsonSerialize() ¶ public
jsonSerialize(): string
Returns a string that should be serialized when converting this object to json
Returns
string
listTimezones() ¶ public static
listTimezones(int|string $filter = null, string $country = null, bool $group = true): array
Get list of timezone identifiers
Parameters
-
int|string
$filter optional A regex to filter identifier Or one of DateTimeZone class constants
-
string
$country optional A two-letter ISO 3166-1 compatible country code. This option is only used when $filter is set to DateTimeZone::PER_COUNTRY
-
bool
$group optional If true (default value) groups the identifiers list by primary region
Returns
array
nice() ¶ public
nice(stringDateTimeZone|null $timezone = null, string|null $locale = null): string
Returns a nicely formatted date string for this object.
The format to be used is stored in the static property Time::niceFormat
.
Parameters
-
stringDateTimeZone|null
$timezone optional Timezone string or DateTimeZone object in which the date will be displayed. The timezone stored for this object will not be changed.
-
string|null
$locale optional The locale name in which the date should be displayed (e.g. pt-BR)
Returns
string
parseDate() ¶ public static
parseDate(string $date, string|int $format = null): static|null
Returns a new Time object after parsing the provided $date string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.
When no $format is provided, the wordFormat
format will be used.
If it was impossible to parse the provided time, null will be returned.
Example:
$time = Time::parseDate('10/13/2013');
$time = Time::parseDate('13 Oct, 2013', 'dd MMM, y');
$time = Time::parseDate('13 Oct, 2013', IntlDateFormatter::SHORT);
Parameters
-
string
$date The date string to parse.
-
string|int
$format optional Any format accepted by IntlDateFormatter.
Returns
static|null
parseDateTime() ¶ public static
parseDateTime(string $time, string|array $format = null): static|null
Returns a new Time object after parsing the provided time string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.
When no $format is provided, the toString
format will be used.
If it was impossible to parse the provided time, null will be returned.
Example:
$time = Time::parseDateTime('10/13/2013 12:54am');
$time = Time::parseDateTime('13 Oct, 2013 13:54', 'dd MMM, y H:mm');
$time = Time::parseDateTime('10/10/2015', [IntlDateFormatter::SHORT, -1]);
Parameters
-
string
$time The time string to parse.
-
string|array
$format optional Any format accepted by IntlDateFormatter.
Returns
static|null
parseTime() ¶ public static
parseTime(string $time, string|int $format = null): static|null
Returns a new Time object after parsing the provided $time string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.
When no $format is provided, the IntlDateFormatter::SHORT format will be used.
If it was impossible to parse the provided time, null will be returned.
Example:
$time = Time::parseDate('11:23pm');
Parameters
-
string
$time The time string to parse.
-
string|int
$format optional Any format accepted by IntlDateFormatter.
Returns
static|null
resetToStringFormat() ¶ public static
resetToStringFormat(): void
Resets the format used to the default when converting an instance of this type to a string
Returns
void
setJsonEncodeFormat() ¶ public static
setJsonEncodeFormat(string|array|int $format): void
Sets the default format used when converting this object to json
Parameters
-
string|array|int
$format Format.
Returns
void
setToStringFormat() ¶ public static
setToStringFormat(string|array|int $format): void
Sets the default format used when type converting instances of this type to string
Parameters
-
string|array|int
$format Format.
Returns
void
timeAgoInWords() ¶ public
timeAgoInWords(array $options = []): string
Returns either a relative or a formatted absolute date depending on the difference between the current time and this object.
Options:
from
=> another Time object representing the "now" timeformat
=> a fall back format if the relative time is longer than the duration specified by endaccuracy
=> Specifies how accurate the date should be described (array)- year => The format if years > 0 (default "day")
- month => The format if months > 0 (default "day")
- week => The format if weeks > 0 (default "day")
- day => The format if weeks > 0 (default "hour")
- hour => The format if hours > 0 (default "minute")
- minute => The format if minutes > 0 (default "minute")
- second => The format if seconds > 0 (default "second")
end
=> The end of relative time tellingrelativeString
=> The printf compatible string when outputting relative timeabsoluteString
=> The printf compatible string when outputting absolute timetimezone
=> The user timezone the timestamp should be formatted in.
Relative dates look something like this:
- 3 weeks, 4 days ago
- 15 seconds ago
Default date formatting is d/M/YY e.g: on 18/2/09. Formatting is done internally using
i18nFormat
, see the method for the valid formatting strings
The returned string includes 'ago' or 'on' and assumes you'll properly add a word like 'Posted ' before the function output.
NOTE: If the difference is one week or more, the lowest level of accuracy is day
Parameters
-
array
$options optional Array of options.
Returns
string
toQuarter() ¶ public
toQuarter(bool $range = false): mixed
Returns the quarter
Parameters
-
bool
$range optional Range.
Returns
mixed
wasWithinLast() ¶ public
wasWithinLast(string|int $timeInterval): bool
Returns true this instance happened within the specified interval
Parameters
-
string|int
$timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
Returns
bool
Property Detail
$_jsonEncodeFormat ¶ protected static
The format to use when when converting this object to json
The format should be either the formatting constants from IntlDateFormatter as described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
It is possible to provide an array of 2 constants. In this case, the first position will be used for formatting the date part of the object and the second position will be used to format the time part.
Type
string|array|int
$_toStringFormat ¶ protected static
The format to use when formatting a time using Cake\I18n\Time::i18nFormat()
and __toString
The format should be either the formatting constants from IntlDateFormatter as described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
It is possible to provide an array of 2 constants. In this case, the first position will be used for formatting the date part of the object and the second position will be used to format the time part.
Type
string|array|int
$defaultLocale ¶ public static
The default locale to be used for displaying formatted date strings.
Type
string
$niceFormat ¶ public static
The format to use when formatting a time using Cake\I18n\Time::nice()
The format should be either the formatting constants from IntlDateFormatter as described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
It is possible to provide an array of 2 constants. In this case, the first position will be used for formatting the date part of the object and the second position will be used to format the time part.
Type
string|array|int
$wordAccuracy ¶ public static
The format to use when formatting a time using Time::timeAgoInWords()
and the difference is less than Time::$wordEnd
Type
array
$wordFormat ¶ public static
The format to use when formatting a time using Cake\I18n\Time::timeAgoInWords()
and the difference is more than Cake\I18n\Time::$wordEnd
Type
string