Trait for date formatting methods shared by both Time & Date.
This trait expects that the implementing class define static::$_toStringFormat.
\IntlDateFormatter[]
In-memory cache of date formatters
string|null
The default locale to be used for displaying formatted date strings.
bool
Whether lenient parsing is enabled for IntlDateFormatter.
Returns a formatted string for this time object using the preferred format and language for the specified locale.
Returns a string that should be serialized when converting this object to JSON
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.
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.
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.
Resets the format used to the default when converting an instance of this type to a string
Sets the default format used when type converting instances of this type to string
__debugInfo()
Returns the data that should be displayed when debugging this object
array
__toString()
_formatObject(mixed $date, mixed $format, ?string $locale)
Returns a translated and localized date string.
Implements what IntlDateFormatter::formatObject() is in PHP 5.5+
\DateTime|\DateTimeImmutable
$date Date.
string|int|int[]
$format Format.
string|null
$locale The locale name in which the date should be displayed.
string
disableLenientParsing()
Enables lenient parsing for locale formats.
enableLenientParsing()
Enables lenient parsing for locale formats.
getDefaultLocale()
Gets the default locale.
string|null
The default locale string to be used or null.
getDiffFormatter()
Get the difference formatter instance.
\Cake\Chronos\DifferenceFormatterInterface
i18nFormat(mixed $format, mixed $timezone, mixed $locale)
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
$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'
You can control the default format used through Time::setToStringFormat()
.
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.
$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 used through Time::setDefaultLocale()
. If empty, the default will be taken from the intl.default_locale
ini config.
string|int|int[]|null
$format optional Format string.
string|\DateTimeZone|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)
string|int
Formatted and translated date string
jsonSerialize()
Returns a string that should be serialized when converting this object to JSON
string|int
lenientParsingEnabled()
Gets whether locale format parsing is set to lenient.
bool
nice(mixed $timezone, mixed $locale)
Returns a nicely formatted date string for this object.
The format to be used is stored in the static property Time::niceFormat
.
string|\DateTimeZone|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)
string
Formatted date string
parseDate(string $date, mixed $format)
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);
string
$date The date string to parse.
string|int|array|null
$format optional Any format accepted by IntlDateFormatter.
static|null
parseDateTime(string $time, mixed $format, mixed $tz)
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.
Unlike DateTime, the time zone of the returned instance is always converted to $tz
(default time zone if null) even if the $time
string specified a time zone. This is a limitation of IntlDateFormatter.
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, IntlDateFormatter::NONE]);
string
$time The time string to parse.
string|int|int[]|null
$format optional Any format accepted by IntlDateFormatter.
\DateTimeZone|string|null
$tz optional The timezone for the instance
static|null
parseTime(string $time, mixed $format)
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');
string
$time The time string to parse.
string|int|null
$format optional Any format accepted by IntlDateFormatter.
static|null
resetToStringFormat()
Resets the format used to the default when converting an instance of this type to a string
setDefaultLocale(?string $locale)
Sets the default locale.
Set to null to use IntlDateFormatter default.
string|null
$locale optional The default locale string to be used.
setDiffFormatter(\Cake\Chronos\DifferenceFormatterInterface $formatter)
Set the difference formatter instance.
\Cake\Chronos\DifferenceFormatterInterface
$formatter The formatter instance when setting.
setJsonEncodeFormat(mixed $format)
mixed
$format setToStringFormat(mixed $format)
Sets the default format used when type converting instances of this type to string
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.
string|int|int[]
$format Format.
In-memory cache of date formatters
\IntlDateFormatter[]
The default locale to be used for displaying formatted date strings.
Use static::setDefaultLocale() and static::getDefaultLocale() instead.
string|null
Whether lenient parsing is enabled for IntlDateFormatter.
Defaults to true which is the default for IntlDateFormatter.
bool
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/trait-Cake.I18n.DateFormatTrait.html