Class CakeTime
Time Helper class for easy use of time data.
Manipulation of time data.
Link: http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: License (http://www.opensource.org/licenses/mit-license.php)
Location: Cake/Utility/CakeTime.php
Properties summary
-
$_time
protected staticTemporary variable containing timestamp value, used internally convertSpecifiers() -
$niceFormat
public staticstring
The format to use when formatting a time usingTimeHelper::nice()
Method Summary
-
__get() public
Magic set method for backward compatibility. -
__set() public
Magic set method for backward compatibility. -
_strftime() protected static
Multibyte wrapper for strftime. -
_translateSpecifier() protected static
Auxiliary function to translate a matched specifier element from a regular expression into a windows safe and i18n aware specifier
-
convert() public static
Converts given time (in server's time zone) to user's local time, given his/her offset from GMT. -
convertSpecifiers() public static
Converts a string representing the format for the function strftime and returns a windows safe and i18n aware format.
-
dayAsSql() public static
Returns a partial SQL string to search for all records between two times occurring on the same day.
-
daysAsSql() public static
Returns a partial SQL string to search for all records between two dates. -
format() public static
Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. This function also accepts a time string and a format string as first and second parameters. In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
-
fromString() public static
Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. -
gmt() public static
Returns gmt as a UNIX timestamp. -
i18nFormat() public static
Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. It take in account the default date format for the current language if a LC_TIME file is used.
-
isThisMonth() public static
Returns true if given datetime string is within this month -
isThisWeek() public static
Returns true if given datetime string is within this week. -
isThisYear() public static
Returns true if given datetime string is within current year. -
isToday() public static
Returns true if given datetime string is today. -
isTomorrow() public static
Returns true if given datetime string is tomorrow. -
nice() public static
Returns a nicely formatted date string for given Datetime string. -
niceShort() public static
Returns a formatted descriptive date string for given datetime string. -
serverOffset() public static
Returns server's offset from GMT in seconds. -
timeAgoInWords() public static
Returns either a relative date or a formatted date depending on the difference between the current time and given datetime. $datetime should be in a strtotime - parsable format, like MySQL's datetime datatype.
-
toAtom() public static
Returns a date formatted for Atom RSS feeds. -
toQuarter() public static
Returns the quarter -
toRSS() public static
Formats date for RSS feeds -
toUnix() public static
Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime(). -
wasWithinLast() public static
Returns true if specified datetime was within the interval specified, else false. -
wasYesterday() public static
Returns true if given datetime string was yesterday.
Method Detail
__get() public ¶
__get( $name )
Magic set method for backward compatibility.
Used by TimeHelper to get static variables in CakeTime
__set() public ¶
__set( $name , $value )
Magic set method for backward compatibility.
Used by TimeHelper to modify static variables in CakeTime
_strftime() protected static ¶
_strftime( string $format , integer $date )
Multibyte wrapper for strftime.
Handles utf8_encoding the result of strftime when necessary.
Parameters
- string $format
- Format string.
- integer $date
- Timestamp to format.
Returns
formatted string with correct encoding.
_translateSpecifier() protected static ¶
_translateSpecifier( array $specifier )
Auxiliary function to translate a matched specifier element from a regular expression into a windows safe and i18n aware specifier
Parameters
- array $specifier
- match from regular expression
Returns
converted element
convert() public static ¶
convert( string $serverTime , integer $userOffset )
Converts given time (in server's time zone) to user's local time, given his/her offset from GMT.
Parameters
- string $serverTime
- UNIX timestamp
- integer $userOffset
- User's offset from GMT (in hours)
Returns
UNIX timestamp
Link
convertSpecifiers() public static ¶
convertSpecifiers( string $format , string $time = null )
Converts a string representing the format for the function strftime and returns a windows safe and i18n aware format.
Parameters
- string $format
Format with specifiers for strftime function. Accepts the special specifier %S which mimics the modifier S for date()
- string $time optional null
- UNIX timestamp
Returns
windows safe and date() function compatible format for strftime
Link
dayAsSql() public static ¶
dayAsSql( string $dateString , string $fieldName , integer $userOffset = null )
Returns a partial SQL string to search for all records between two times occurring on the same day.
Parameters
- string $dateString
- Datetime string or Unix timestamp
- string $fieldName
- Name of database field to compare with
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Partial SQL string.
Link
daysAsSql() public static ¶
daysAsSql( string $begin , string $end , string $fieldName , integer $userOffset = null )
Returns a partial SQL string to search for all records between two dates.
Parameters
- string $begin
- Datetime string or Unix timestamp
- string $end
- Datetime string or Unix timestamp
- string $fieldName
- Name of database field to compare with
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Partial SQL string.
Link
format() public static ¶
format( string $format , string $date = null , boolean $invalid = false , integer $userOffset = null )
Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. This function also accepts a time string and a format string as first and second parameters. In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
Parameters
- string $format
- date format string (or a DateTime string)
- string $date optional null
- Datetime string (or a date format string)
- boolean $invalid optional false
- flag to ignore results of fromString == false
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Formatted date string
Link
fromString() public static ¶
fromString( string $dateString , integer $userOffset = null )
Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
Parameters
- string $dateString
- Datetime string
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Parsed timestamp
Link
gmt() public static ¶
gmt( string $string = null )
Returns gmt as a UNIX timestamp.
Parameters
- string $string optional null
- UNIX timestamp or a valid strtotime() date string
Returns
UNIX timestamp
Link
i18nFormat() public static ¶
i18nFormat( string $date , string $format = null , boolean $invalid = false , integer $userOffset = null )
Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. It take in account the default date format for the current language if a LC_TIME file is used.
Parameters
- string $date
- Datetime string
- string $format optional null
- strftime format string.
- boolean $invalid optional false
- flag to ignore results of fromString == false
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Formatted and translated date string
Link
isThisMonth() public static ¶
isThisMonth( string $dateString , integer $userOffset = null )
Returns true if given datetime string is within this month
Parameters
- string $dateString
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
True if datetime string is within current month
Link
isThisWeek() public static ¶
isThisWeek( string $dateString , integer $userOffset = null )
Returns true if given datetime string is within this week.
Parameters
- string $dateString
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
True if datetime string is within current week
Link
isThisYear() public static ¶
isThisYear( string $dateString , integer $userOffset = null )
Returns true if given datetime string is within current year.
Parameters
- string $dateString
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
True if datetime string is within current year
Link
isToday() public static ¶
isToday( string $dateString , integer $userOffset = null )
Returns true if given datetime string is today.
Parameters
- string $dateString
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
True if datetime string is today
Link
isTomorrow() public static ¶
isTomorrow( string $dateString , integer $userOffset = null )
Returns true if given datetime string is tomorrow.
Parameters
- string $dateString
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
True if datetime string was yesterday
Link
nice() public static ¶
nice( string $dateString = null , integer $userOffset = null , string $format = null )
Returns a nicely formatted date string for given Datetime string.
See http://php.net/manual/en/function.strftime.php for information on formatting using locale strings.
Parameters
- string $dateString optional null
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
- string $format optional null
- The format to use. If null,
TimeHelper::$niceFormat
is used
Returns
Formatted date string
Link
niceShort() public static ¶
niceShort( string $dateString = null , integer $userOffset = null )
Returns a formatted descriptive date string for given datetime string.
If the given date is today, the returned string could be "Today, 16:54". If the given date was yesterday, the returned string could be "Yesterday, 16:54". If $dateString's year is the current year, the returned string does not include mention of the year.
Parameters
- string $dateString optional null
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Described, relative date string
Link
serverOffset() public static ¶
serverOffset( )
Returns server's offset from GMT in seconds.
Returns
Offset
Link
timeAgoInWords() public static ¶
timeAgoInWords( string $dateTime , array $options = array() )
Returns either a relative date or a formatted date depending on the difference between the current time and given datetime. $datetime should be in a strtotime - parsable format, like MySQL's datetime datatype.
Options:
format
=> a fall back format if the relative time is longer than the duration specified by endend
=> The end of relative time tellinguserOffset
=> Users offset from GMT (in hours)
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
The returned string includes 'ago' or 'on' and assumes you'll properly add a word like 'Posted ' before the function output.
Parameters
- string $dateTime
- Datetime string or Unix timestamp
- array $options optional array()
- Default format if timestamp is used in $dateString
Returns
Relative time string.
Link
toAtom() public static ¶
toAtom( string $dateString , integer $userOffset = null )
Returns a date formatted for Atom RSS feeds.
Parameters
- string $dateString
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Formatted date string
Link
toQuarter() public static ¶
toQuarter( string $dateString , boolean $range = false )
Returns the quarter
Parameters
- string $dateString
- boolean $range optional false
- if true returns a range in Y-m-d format
Returns
1, 2, 3, or 4 quarter of year or array if $range true
Link
toRSS() public static ¶
toRSS( string $dateString , integer $userOffset = null )
Formats date for RSS feeds
Parameters
- string $dateString
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Formatted date string
Link
toUnix() public static ¶
toUnix( string $dateString , integer $userOffset = null )
Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
Parameters
- string $dateString
- Datetime string to be represented as a Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Unix timestamp
Link
wasWithinLast() public static ¶
wasWithinLast( mixed $timeInterval , mixed $dateString , integer $userOffset = null )
Returns true if specified datetime was within the interval specified, else false.
Parameters
- mixed $timeInterval
the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
- mixed $dateString
- the datestring or unix timestamp to compare
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Link
wasYesterday() public static ¶
wasYesterday( string $dateString , integer $userOffset = null )
Returns true if given datetime string was yesterday.
Parameters
- string $dateString
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
True if datetime string was yesterday
Link
Properties detail
$_time ¶
Temporary variable containing timestamp value, used internally convertSpecifiers()
null
$niceFormat ¶
The format to use when formatting a time using TimeHelper::nice()
The format should use the locale strings as defined in the PHP docs under
strftime
(http://php.net/manual/en/function.strftime.php)
See
'%a, %b %eS %Y, %H:%M'