Class TimeHelper
Time Helper class for easy use of time data.
Manipulation of time data.
- AppHelper
- TimeHelper
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: The MIT License
Location: view/helpers/time.php
Method Summary
-
convert() public
Converts given time (in server's time zone) to user's local time, given his/her offset from GMT. -
dayAsSql() public
Returns a partial SQL string to search for all records between two times occurring on the same day.
-
daysAsSql() public
Returns a partial SQL string to search for all records between two dates. -
format() public
Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. -
fromString() public
Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. -
gmt() public
Returns gmt, given either a UNIX timestamp or a valid strtotime() date string. -
isThisMonth() public
Returns true if given datetime string is within this month -
isThisWeek() public
Returns true if given datetime string is within this week -
isThisYear() public
Returns true if given datetime string is within current year. -
isToday() public
Returns true if given datetime string is today. -
isTomorrow() public
Returns true if given datetime string is tomorrow. -
nice() public
Returns a nicely formatted date string for given Datetime string. -
niceShort() public
Returns a formatted descriptive date string for given datetime string. -
relativeTime() public
Alias for timeAgoInWords -
serverOffset() public
Returns server's offset from GMT in seconds. -
timeAgoInWords() public
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
Returns a date formatted for Atom RSS feeds. -
toQuarter() public
Returns the quart -
toRSS() public
Formats date for RSS feeds -
toUnix() public
Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime(). -
wasWithinLast() public
Returns true if specified datetime was within the interval specified, else false. -
wasYesterday() public
Returns true if given datetime string was yesterday.
Method Detail
convert() public ¶
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
dayAsSql() public ¶
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.
daysAsSql() public ¶
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
- $dateString 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.
format() public ¶
format( string $format , string $date , boolean $invalid = false , integer $userOffset = null )
Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
Parameters
- string $format
- date format string. defaults to 'd-m-Y'
- string $date
- $dateString Datetime 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
fromString() public ¶
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
gmt() public ¶
gmt( string $string = null )
Returns gmt, given either a UNIX timestamp or a valid strtotime() date string.
Parameters
- string $string optional null
- $dateString Datetime string
Returns
Formatted date string
isThisMonth() public ¶
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
isThisWeek() public ¶
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
isThisYear() public ¶
isThisYear( string $dateString , $userOffset = null )
Returns true if given datetime string is within current year.
Parameters
- string $dateString
- Datetime string or Unix timestamp
- $userOffset optional null
Returns
True if datetime string is within current year
isToday() public ¶
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
isTomorrow() public ¶
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
nice() public ¶
nice( string $dateString = null , integer $userOffset = null )
Returns a nicely formatted date string for given Datetime string.
Parameters
- string $dateString optional null
- Datetime string or Unix timestamp
- integer $userOffset optional null
- User's offset from GMT (in hours)
Returns
Formatted date string
niceShort() public ¶
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
relativeTime() public ¶
relativeTime( mixed $dateTime , mixed $options = array() )
Alias for timeAgoInWords
Parameters
- mixed $dateTime
- Datetime string (strtotime-compatible) or Unix timestamp
- mixed $options optional array()
Default format string, if timestamp is used in $dateTime, or an array of options to be passed on to timeAgoInWords().
Returns
Relative time string.
See
serverOffset() public ¶
serverOffset( )
Returns server's offset from GMT in seconds.
Returns
Offset
timeAgoInWords() public ¶
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 end
- 'end' => The end of relative time telling
- 'userOffset' => 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
- $dateString Datetime string or Unix timestamp
- array $options optional array()
- Default format if timestamp is used in $dateString
Returns
Relative time string.
toAtom() public ¶
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
toQuarter() public ¶
toQuarter( string $dateString , boolean $range = false )
Returns the quart
Parameters
- string $dateString
- boolean $range optional false
- if true returns a range in Y-m-d format
Returns
True if datetime string is within current week
toRSS() public ¶
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
toUnix() public ¶
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
wasWithinLast() public ¶
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
wasYesterday() public ¶
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