Class TimeHelper

Time Helper class for easy use of time data.

Manipulation of time data.

AppHelper
Extended by TimeHelper
Package: Cake\View\Helper
Link: https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
See: CakeTime
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/View/Helper/TimeHelper.php

Properties summary

Method Summary

  • __call() public
    Call methods from CakeTime utility class
  • __construct() public
    Constructor
  • __get() public
    Magic accessor for attributes that were deprecated.
  • __isset() public
    Magic isset check for deprecated attributes.
  • __set() public
    Magic accessor for deprecated attributes.
  • convert() public
    Converts given time (in server's time zone) to user's local time, given his/her timezone.
  • Converts a string representing the format for the function strftime and returns a Windows safe and i18n aware format.

  • 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. 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
    Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
  • gmt() public
    Returns gmt as a UNIX timestamp.
  • i18nFormat() public

    Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. It takes into account the default date format for the current language if a LC_TIME file is used.

  • 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.
  • Returns true if specified datetime is within the interval specified, else false.
  • nice() public
    Returns a nicely formatted date string for given Datetime string.
  • niceShort() public
    Returns a formatted descriptive date string for given datetime string.
  • Returns server's offset
  • Formats a date into a phrase expressing the relative time.
  • toAtom() public
    Returns a date formatted for Atom RSS feeds.
  • toQuarter() public
    Returns the quarter
  • toRSS() public
    Formats date for RSS feeds
  • toUnix() public
    Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
  • Returns true if specified datetime was within the interval specified, else false.
  • Returns true if given datetime string was yesterday.

Method Detail

__call()source public

__call( string $method , array $params )

Call methods from CakeTime utility class

Parameters

string $method
Method to call.
array $params
Parameters to pass to method.

Returns

mixed
Whatever is returned by called method, or false on failure

__construct()source public

__construct( View $View , array $settings array() )

Constructor

Settings:

  • engine Class name to use to replace CakeTime functionality The class needs to be placed in the Utility directory.

Parameters

View $View
the view object the helper is attached to.
array $settings optional array()
Settings array

Throws

CakeException
When the engine class could not be found.

__get()source public

__get( string $name )

Magic accessor for attributes that were deprecated.

Parameters

string $name
Name of the attribute to get.

Returns

mixed

__isset()source public

__isset( string $name )

Magic isset check for deprecated attributes.

Parameters

string $name
Name of the attribute to check.

Returns

boolean|null

__set()source public

__set( string $name , string $value )

Magic accessor for deprecated attributes.

Parameters

string $name
Name of the attribute to set.
string $value
Value of the attribute to set.

convert()source public

convert( string $serverTime , string|DateTimeZone $timezone )

Converts given time (in server's time zone) to user's local time, given his/her timezone.

Parameters

string $serverTime
UNIX timestamp
string|DateTimeZone $timezone
User's timezone string or DateTimeZone object

Returns

integer
UNIX timestamp

See

CakeTime::convert()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

convertSpecifiers()source public

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

See

CakeTime::convertSpecifiers()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

dayAsSql()source public

dayAsSql( integer|string|DateTime $dateString , string $fieldName , string|DateTimeZone $timezone null )

Returns a partial SQL string to search for all records between two times occurring on the same day.

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string $fieldName
Name of database field to compare with
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

string
Partial SQL string.

See

CakeTime::dayAsSql()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

daysAsSql()source public

daysAsSql( integer|string|DateTime $begin , integer|string|DateTime $end , string $fieldName , string|DateTimeZone $timezone null )

Returns a partial SQL string to search for all records between two dates.

Parameters

integer|string|DateTime $begin
UNIX timestamp, strtotime() valid string or DateTime object
integer|string|DateTime $end
UNIX timestamp, strtotime() valid string or DateTime object
string $fieldName
Name of database field to compare with
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

string
Partial SQL string.

See

CakeTime::daysAsSql()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

format()source public

format( integer|string|DateTime $format , integer|string|DateTime $date null , boolean $invalid false , string|DateTimeZone $timezone 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()

Examples

Create localized & formatted time:

$this->Time->format('2012-02-15', '%m-%d-%Y'); // returns 02-15-2012
  $this->Time->format('2012-02-15 23:01:01', '%c'); // returns preferred date and time based on configured locale
  $this->Time->format('0000-00-00', '%d-%m-%Y', 'N/A'); // return N/A because an invalid date was passed
  $this->Time->format('2012-02-15 23:01:01', '%c', 'N/A', 'America/New_York'); // converts passed date to timezone

Parameters

integer|string|DateTime $format
date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
integer|string|DateTime $date optional null
UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
boolean $invalid optional false
flag to ignore results of fromString == false
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

string
Formatted date string

See

CakeTime::format()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

fromString()source public

fromString( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

string
Parsed timestamp

See

CakeTime::fromString()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

gmt()source public

gmt( integer|string|DateTime $string null )

Returns gmt as a UNIX timestamp.

Parameters

integer|string|DateTime $string optional null
UNIX timestamp, strtotime() valid string or DateTime object

Returns

integer
UNIX timestamp

See

CakeTime::gmt()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

i18nFormat()source public

i18nFormat( integer|string|DateTime $date , string $format null , boolean $invalid false , string|DateTimeZone $timezone null )

Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. It takes into account the default date format for the current language if a LC_TIME file is used.

Parameters

integer|string|DateTime $date
UNIX timestamp, strtotime() valid string or DateTime object
string $format optional null
strftime format string.
boolean $invalid optional false
flag to ignore results of fromString == false
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

string
Formatted and translated date string

See

CakeTime::i18nFormat()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

isThisMonth()source public

isThisMonth( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns true if given datetime string is within this month

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

boolean
True if datetime string is within current month

See

CakeTime::isThisMonth()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

isThisWeek()source public

isThisWeek( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns true if given datetime string is within this week.

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

boolean
True if datetime string is within current week

See

CakeTime::isThisWeek()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

isThisYear()source public

isThisYear( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns true if given datetime string is within current year.

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

boolean
True if datetime string is within current year

See

CakeTime::isThisYear()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

isToday()source public

isToday( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns true if given datetime string is today.

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

boolean
True if datetime string is today

See

CakeTime::isToday()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

isTomorrow()source public

isTomorrow( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns true if given datetime string is tomorrow.

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

boolean
True if datetime string was yesterday

See

CakeTime::isTomorrow()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

isWithinNext()source public

isWithinNext( string|integer $timeInterval , integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns true if specified datetime is within the interval specified, else false.

Parameters

string|integer $timeInterval

the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

boolean

See

CakeTime::isWithinLast()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

nice()source public

nice( integer|string|DateTime $dateString null , string|DateTimeZone $timezone null , string $format null )

Returns a nicely formatted date string for given Datetime string.

Parameters

integer|string|DateTime $dateString optional null
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object
string $format optional null
The format to use. If null, CakeTime::$niceFormat is used

Returns

string
Formatted date string

See

CakeTime::nice()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

niceShort()source public

niceShort( integer|string|DateTime $dateString null , string|DateTimeZone $timezone null )

Returns a formatted descriptive date string for given datetime string.

Parameters

integer|string|DateTime $dateString optional null
UNIX timestamp, strtotime() valid string or DateTime object.
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

string
Described, relative date string

See

CakeTime::niceShort()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

serverOffset()source public

serverOffset( )

Returns server's offset

Returns

integer
Offset

See

CakeTime::serverOffset()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

timeAgoInWords()source public

timeAgoInWords( integer|string|DateTime $dateTime , array $options array() )

Formats a date into a phrase expressing the relative time.

Addition options

  • element - The element to wrap the formatted time in. Has a few additional options:
    • tag - The tag to use, defaults to 'span'.
    • class - The class name to use, defaults to time-ago-in-words.
    • title - Defaults to the $dateTime input.

Parameters

integer|string|DateTime $dateTime
UNIX timestamp, strtotime() valid string or DateTime object
array $options optional array()
Default format if timestamp is used in $dateString

Returns

string
Relative time string.

See

CakeTime::timeAgoInWords()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

toAtom()source public

toAtom( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns a date formatted for Atom RSS feeds.

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

string
Formatted date string

See

CakeTime::toAtom()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

toQuarter()source public

toQuarter( integer|string|DateTime $dateString , boolean $range false )

Returns the quarter

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
boolean $range optional false
if true returns a range in Y-m-d format

Returns

integer|array
1, 2, 3, or 4 quarter of year or array if $range true

See

CakeTime::toQuarter()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

toRSS()source public

toRSS( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Formats date for RSS feeds

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

string
Formatted date string

See

CakeTime::toRSS()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

toUnix()source public

toUnix( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

integer
Unix timestamp

See

CakeTime::toUnix()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting

wasWithinLast()source public

wasWithinLast( string|integer $timeInterval , integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns true if specified datetime was within the interval specified, else false.

Parameters

string|integer $timeInterval

the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

boolean

See

CakeTime::wasWithinLast()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

wasYesterday()source public

wasYesterday( integer|string|DateTime $dateString , string|DateTimeZone $timezone null )

Returns true if given datetime string was yesterday.

Parameters

integer|string|DateTime $dateString
UNIX timestamp, strtotime() valid string or DateTime object
string|DateTimeZone $timezone optional null
User's timezone string or DateTimeZone object

Returns

boolean
True if datetime string was yesterday

See

CakeTime::wasYesterday()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time

Properties detail

$_enginesource

protected CakeTime

CakeTime instance

null

© 2005–2017 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/2.10/class-TimeHelper.html