Class FrozenDate
Extends the Date class provided by Chronos.
Adds handy methods and locale-aware formatting helpers
This object provides an immutable variant of Cake\I18n\Date
- Cake\Chronos\Date
- Cake\I18n\FrozenDate implements JsonSerializable uses Cake\I18n\DateFormatTrait
Properties summary
-
$_toStringFormat
protected staticstring|array|integer
The format to use when formatting a time using
Cake\I18n\Date::i18nFormat()
and__toString
-
$niceFormat
public staticstring|array|integer
The format to use when formatting a time usingCake\I18n\Date::nice()
-
$wordAccuracy
public staticarray
The format to use when formatting a time using
Date::timeAgoInWords()
and the difference is less thanDate::$wordEnd
-
$wordEnd
public staticstring
The end of relative time telling -
$wordFormat
public staticstring|array|integer
The format to use when formatting a time using
Cake\I18n\Date::timeAgoInWords()
and the difference is more thanCake\I18n\Date::$wordEnd
Inherited Properties
Method Summary
-
timeAgoInWords() public
Returns either a relative or a formatted absolute date depending on the difference between the current date and this object.
Method Detail
timeAgoInWords() public ¶
timeAgoInWords( array $options [] )
Returns either a relative or a formatted absolute date depending on the difference between the current date and this object.
Options:
from
=> another Date object representing the "now" dateformat
=> 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 "day")
end
=> The end of relative date tellingrelativeString
=> The printf compatible string when outputting relative dateabsoluteString
=> The printf compatible string when outputting absolute datetimezone
=> The user timezone the timestamp should be formatted in.
Relative dates look something like this:
- 3 weeks, 4 days ago
- 1 day 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
Relative time string.
Methods used from Cake\I18n\DateFormatTrait
__debugInfo() public ¶
__debugInfo( )
Returns the data that should be displayed when debugging this object
Returns
_formatObject() protected ¶
_formatObject( DateTime $date , string|integer|array $format , string $locale )
Returns a translated and localized date string. Implements what IntlDateFormatter::formatObject() is in PHP 5.5+
Parameters
- DateTime $date
- Date.
- string|integer|array $format
- Format.
- string $locale
- The locale name in which the date should be displayed.
Returns
diffFormatter() public static ¶
diffFormatter( Cake\I18n\RelativeTimeFormatter|null $formatter null )
Get the difference formatter instance or overwrite the current one.
Parameters
- Cake\I18n\RelativeTimeFormatter|null $formatter optional null
- The formatter instance when setting.
Returns
The formatter instance.
getDefaultLocale() public static ¶
getDefaultLocale( )
Gets the default locale.
Returns
The default locale string to be used or null.
i18nFormat() public ¶
i18nFormat( string|integer|null $format null , string|DateTimeZone|null $timezone null , string|null $locale null )
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.
Additional to IntlDateFormatter
constants and date formatting string you can use
Time::UNIX_TIMESTAMP_FORMAT to get a unix timestamp
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' $time->i18nFormat(Time::UNIX_TIMESTAMP_FORMAT); // outputs '1398031800'
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 https://secure.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|integer|null $format optional null
- Format string.
- string|DateTimeZone|null $timezone optional null
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 null
- The locale name in which the date should be displayed (e.g. pt-BR)
Returns
Formatted and translated date string
jsonSerialize() public ¶
jsonSerialize( )
Returns a string that should be serialized when converting this object to json
Returns
nice() public ¶
nice( string|DateTimeZone|null $timezone null , string|null $locale null )
Returns a nicely formatted date string for this object.
The format to be used is stored in the static property Time::niceFormat
.
Parameters
- string|DateTimeZone|null $timezone optional null
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 null
- The locale name in which the date should be displayed (e.g. pt-BR)
Returns
Formatted date string
parseDate() public static ¶
parseDate( string $date , string|integer|null $format 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|integer|null $format optional null
- Any format accepted by IntlDateFormatter.
Returns
parseDateTime() public static ¶
parseDateTime( string $time , string|array|null $format 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|null $format optional null
- Any format accepted by IntlDateFormatter.
Returns
parseTime() public static ¶
parseTime( string $time , string|integer|null $format 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::parseTime('11:23pm');
Parameters
- string $time
- The time string to parse.
- string|integer|null $format optional null
- Any format accepted by IntlDateFormatter.
Returns
resetToStringFormat() public static ¶
resetToStringFormat( )
Resets the format used to the default when converting an instance of this type to a string
setDefaultLocale() public static ¶
setDefaultLocale( string|null $locale null )
Sets the default locale.
Parameters
- string|null $locale optional null
- The default locale string to be used or null.
setJsonEncodeFormat() public static ¶
setJsonEncodeFormat( string|array|integer $format )
Sets the default format used when converting this object to json
Parameters
- string|array|integer $format
- Format.
setToStringFormat() public static ¶
setToStringFormat( string|array|integer $format )
Sets the default format used when type converting instances of this type to string
Parameters
- string|array|integer $format
- Format.
Properties detail
$_toStringFormat ¶
The format to use when formatting a time using Cake\I18n\Date::i18nFormat()
and __toString
The format should be either the formatting constants from IntlDateFormatter as described in (https://secure.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.
See
[IntlDateFormatter::SHORT, -1]
$niceFormat ¶
The format to use when formatting a time using Cake\I18n\Date::nice()
The format should be either the formatting constants from IntlDateFormatter as described in (https://secure.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.
See
[IntlDateFormatter::MEDIUM, -1]
$wordAccuracy ¶
The format to use when formatting a time using Date::timeAgoInWords()
and the difference is less than Date::$wordEnd
See
[ 'year' => 'day', 'month' => 'day', 'week' => 'day', 'day' => 'day', 'hour' => 'day', 'minute' => 'day', 'second' => 'day', ]
$wordEnd ¶
The end of relative time telling
See
'+1 month'
$wordFormat ¶
The format to use when formatting a time using Cake\I18n\Date::timeAgoInWords()
and the difference is more than Cake\I18n\Date::$wordEnd
See
[IntlDateFormatter::SHORT, -1]