Extends the built-in DateTime class to provide handy methods and locale-aware formatting helpers
__construct( mixed $time null , mixed $tz null )
__debugInfo( )
Returns the data that should be displayed when debugging this object
array
__toString( )
_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+
$date
$format
$locale
string
diffForHumans( Carbon\Carbon\Carbon $other null , array $options [] )
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.
$other
optional null $options
optional [] string
Cake\I18n\Time::timeAgoInWords()
fromNow( DateTime|DateTimeImmutable $datetime )
Convenience method for getting the remaining time from a given time.
$datetime
DateInterval|boolean
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.
$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.
$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.
$format
optional null $timezone
optional null $locale
optional null string
isThisMonth( )
Returns true if this object represents a date within the current month
boolean
isThisWeek( )
Returns true if this object represents a date within the current week
boolean
isThisYear( )
Returns true if this object represents a date within the current year
boolean
isWithinNext( string|integer $timeInterval )
Returns true this instance will happen within the specified interval
$timeInterval
boolean
jsonSerialize( )
Returns a string that should be serialized when converting this object to json
string
JsonSerializable::jsonSerialize()
listTimezones( integer|string $filter null , string $country null , boolean|array $options [] )
Get list of timezone identifiers
$filter
optional null $country
optional null $options
optional [] group
, abbr
, before
, and after
keys. Setting group
and abbr
to true will group results and append timezone abbreviation in the display value. Set before
and after
to customize the abbreviation wrapper.array
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
.
$timezone
optional null $locale
optional null string
parseDate( string $date , string|integer $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);
$date
$format
optional null static|null
parseDateTime( string $time , string|array $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]);
$time
$format
optional null static|null
parseTime( string $time , string|integer $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::parseDate('11:23pm');
$time
$format
optional null static|null
resetToStringFormat( )
Resets the format used to the default when converting an instance of this type to a string
setJsonEncodeFormat( string|array|integer $format )
Sets the default format used when converting this object to json
$format
setToStringFormat( string|array|integer $format )
Sets the default format used when type converting instances of this type to string
$format
timeAgoInWords( array $options [] )
Returns either a relative or a formatted absolute date depending on the difference between the current time and this object.
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:
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
$options
optional [] string
toQuarter( boolean $range false )
Returns the quarter
$range
optional false integer|array
wasWithinLast( string|integer $timeInterval )
Returns true this instance happened within the specified interval
$timeInterval
boolean
© 2005–2016 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.
http://api.cakephp.org/3.1/class-Cake.I18n.Time.html