CakeTime
-
class CakeTime
If you need TimeHelper
functionalities outside of a View
,
use the CakeTime
class:
class UsersController extends AppController {
public $components = array('Auth');
public function afterLogin() {
App::uses('CakeTime', 'Utility');
if (CakeTime::isToday($this->Auth->user('date_of_birth']))) {
// greet user with a happy birthday message
$this->Session->setFlash(__('Happy birthday you...'));
}
}
}
New in version 2.1: CakeTime
has been factored out from TimeHelper
.
Testing Time
-
CakeTime::isToday($dateString, $timezone = NULL)
-
CakeTime::isThisWeek($dateString, $timezone = NULL)
-
CakeTime::isThisMonth($dateString, $timezone = NULL)
-
CakeTime::isThisYear($dateString, $timezone = NULL)
-
CakeTime::wasYesterday($dateString, $timezone = NULL)
-
CakeTime::isTomorrow($dateString, $timezone = NULL)
-
CakeTime::isFuture($dateString, $timezone = NULL)
-
-
CakeTime::isPast($dateString, $timezone = NULL)
-
-
CakeTime::wasWithinLast($timeInterval, $dateString, $timezone = NULL)
Changed in version 2.2: $timezone
parameter replaces $userOffset
parameter used in 2.1 and below.
New in version 2.2: $dateString
parameter now also accepts a DateTime object.
All of the above functions return true or false when passed a date
string. wasWithinLast
takes an additional $timeInterval
option:
// called via TimeHelper
$this->Time->wasWithinLast($timeInterval, $dateString);
// called as CakeTime
App::uses('CakeTime', 'Utility');
CakeTime::wasWithinLast($timeInterval, $dateString);
wasWithinLast
takes a time interval which is a string in the
format “3 months” and accepts a time interval of seconds, minutes,
hours, days, weeks, months and years (plural and not). If a time
interval is not recognized (for example, if it is mistyped) then it
will default to days.