CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeNumber
  • CakeTime
  • ClassRegistry
  • Debugger
  • File
  • Folder
  • Inflector
  • ObjectCollection
  • Sanitize
  • Security
  • Set
  • String
  • Validation
  • Xml

Class CakeTime

Time Helper class for easy use of time data.

Manipulation of time data.

Package: Cake\Utility
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 static
    Temporary variable containing timestamp value, used internally convertSpecifiers()
  • $niceFormat public static
    string
    The format to use when formatting a time using TimeHelper::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
string
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
string
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
integer
UNIX timestamp
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
windows safe and date() function compatible format for strftime
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
Partial SQL string.
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
Partial SQL string.
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
Formatted date string
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
Parsed timestamp
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
integer
UNIX timestamp
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
Formatted and translated date string
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
boolean
True if datetime string is within current month
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

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
boolean
True if datetime string is within current week
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

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
boolean
True if datetime string is within current year
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

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
boolean
True if datetime string is today
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

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
boolean
True if datetime string was yesterday
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

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
string
Formatted date string
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
Described, relative date string
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

serverOffset() public static ¶

serverOffset( )

Returns server's offset from GMT in seconds.

Returns
integer
Offset
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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 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
Datetime string or Unix timestamp
array $options optional array()
Default format if timestamp is used in $dateString
Returns
string
Relative time string.
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
Formatted date string
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
mixed
1, 2, 3, or 4 quarter of year or array if $range true
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
string
Formatted date string
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
integer
Unix timestamp
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

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
boolean
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

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
boolean
True if datetime string was yesterday
Link
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

Properties detail

$_time ¶

protected static

Temporary variable containing timestamp value, used internally convertSpecifiers()

null

$niceFormat ¶

public static string

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
TimeHelper::format()
'%a, %b %eS %Y, %H:%M'
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs