cakephp2-php8/lib/Cake/View/Helper/TimeHelper.php

403 lines
13 KiB
PHP
Raw Normal View History

<?php
/**
* Time Helper class file.
*
2010-10-03 16:38:58 +00:00
* PHP 5
*
2009-11-06 06:46:59 +00:00
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
2011-05-29 21:31:39 +00:00
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
2011-05-29 21:31:39 +00:00
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
2009-11-06 06:00:11 +00:00
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View.Helper
* @since CakePHP(tm) v 0.10.0.1076
2009-11-06 06:51:51 +00:00
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
2012-02-07 01:01:43 +00:00
App::uses('CakeTime', 'Utility');
2011-11-21 15:18:12 +00:00
App::uses('Multibyte', 'I18n');
2010-12-04 18:10:24 +00:00
App::uses('AppHelper', 'View/Helper');
/**
* Time Helper class for easy use of time data.
*
* Manipulation of time data.
*
* @package Cake.View.Helper
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
2012-02-07 01:01:43 +00:00
* @see CakeTime
*/
class TimeHelper extends AppHelper {
/**
2012-02-07 01:01:43 +00:00
* CakeTime instance
*/
2012-02-07 01:01:43 +00:00
protected $_CakeTime = null;
/**
* Constructor
*
* @param View $View the view object the helper is attached to.
* @param array $settings Settings array Settings array
*/
public function __construct(View $View, $settings = array()) {
$settings = Set::merge(array('engine' => 'CakeTime'), $settings);
parent::__construct($View, $settings);
$engineClass = $settings['engine'];
App::uses($engineClass, 'Utility');
if (class_exists($engineClass)) {
$this->_CakeTime = new $engineClass($settings);
} else {
throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
}
}
/**
2012-02-07 01:01:43 +00:00
* Magic accessor for deprecated attributes.
*
2012-02-07 01:01:43 +00:00
* @param string $name Name of the attribute to set.
* @param string $value Value of the attribute to set.
* @return mixed
*/
2012-02-07 01:01:43 +00:00
public function __set($name, $value) {
switch ($name) {
case 'niceFormat':
$this->_CakeTime->{$name} = $value;
break;
2012-02-07 01:01:43 +00:00
default:
$this->{$name} = $value;
break;
}
}
/**
2012-02-07 01:01:43 +00:00
* Magic isset check for deprecated attributes.
*
2012-02-07 01:01:43 +00:00
* @param string $name Name of the attribute to check.
* @return boolean
*/
2012-02-07 01:01:43 +00:00
public function __isset($name) {
if (isset($this->{$name})) {
return true;
}
2012-02-07 01:01:43 +00:00
$magicGet = array('niceFormat');
if (in_array($name, $magicGet)) {
return $this->__get($name) !== null;
}
return null;
}
/**
* Magic accessor for attributes that were deprecated.
*
* @param string $name Name of the attribute to get.
* @return mixed
*/
public function __get($name) {
if (isset($this->_CakeTime->{$name})) {
return $this->_CakeTime->{$name};
}
$magicGet = array('niceFormat');
if (in_array($name, $magicGet)) {
return $this->_CakeTime->{$name};
}
return null;
}
/**
* Call methods from CakeTime utility class
*/
public function __call($method, $params) {
return call_user_func_array(array($this->_CakeTime, $method), $params);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::convertSpecifiers()
*
* @param string $format Format with specifiers for strftime function.
* Accepts the special specifier %S which mimics the modifier S for date()
* @param string $time UNIX timestamp
* @return string windows safe and date() function compatible format for strftime
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function convertSpecifiers($format, $time = null) {
return $this->_CakeTime->convertSpecifiers($format, $time);
}
/**
* @see CakeTime::convert()
*
* @param string $serverTime UNIX timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @return integer UNIX timestamp
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function convert($serverTime, $userOffset) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->convert($serverTime, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::serverOffset()
*
* @return integer Offset
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function serverOffset() {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->serverOffset();
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::fromString()
*
* @param string $dateString Datetime string
* @param integer $userOffset User's offset from GMT (in hours)
* @return string Parsed timestamp
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function fromString($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->fromString($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::nice()
*
* @param string $dateString Datetime string or Unix timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used
* @return string Formatted date string
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function nice($dateString = null, $userOffset = null, $format = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->nice($dateString, $userOffset, $format);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::niceShort()
*
* @param string $dateString Datetime string or Unix timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @return string Described, relative date string
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function niceShort($dateString = null, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->niceShort($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::daysAsSql()
*
2011-07-29 02:45:47 +00:00
* @param string $begin Datetime string or Unix timestamp
* @param string $end Datetime string or Unix timestamp
* @param string $fieldName Name of database field to compare with
* @param integer $userOffset User's offset from GMT (in hours)
* @return string Partial SQL string.
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function daysAsSql($begin, $end, $fieldName, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->daysAsSql($begin, $end, $fieldName, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::dayAsSql()
*
* @param string $dateString Datetime string or Unix timestamp
* @param string $fieldName Name of database field to compare with
* @param integer $userOffset User's offset from GMT (in hours)
* @return string Partial SQL string.
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function dayAsSql($dateString, $fieldName, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->dayAsSql($dateString, $fieldName, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::isToday()
*
* @param string $dateString Datetime string or Unix timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @return boolean True if datetime string is today
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isToday($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->isToday($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::isThisWeek()
*
* @param string $dateString
* @param integer $userOffset User's offset from GMT (in hours)
* @return boolean True if datetime string is within current week
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isThisWeek($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->isThisWeek($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::isThisMonth()
*
* @param string $dateString
* @param integer $userOffset User's offset from GMT (in hours)
* @return boolean True if datetime string is within current month
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isThisMonth($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->isThisMonth($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::isThisYear()
*
* @param string $dateString Datetime string or Unix timestamp
2011-07-29 02:45:47 +00:00
* @param integer $userOffset User's offset from GMT (in hours)
* @return boolean True if datetime string is within current year
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isThisYear($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->isThisYear($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::wasYesterday()
*
* @param string $dateString Datetime string or Unix timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @return boolean True if datetime string was yesterday
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*
*/
public function wasYesterday($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->wasYesterday($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::isTomorrow()
*
* @param string $dateString Datetime string or Unix timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @return boolean True if datetime string was yesterday
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isTomorrow($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->isTomorrow($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::toQuarter()
*
* @param string $dateString
* @param boolean $range if true returns a range in Y-m-d format
* @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function toQuarter($dateString, $range = false) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->toQuarter($dateString, $range);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::toUnix()
*
* @param string $dateString Datetime string to be represented as a Unix timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @return integer Unix timestamp
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function toUnix($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->toUnix($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::toAtom()
*
* @param string $dateString Datetime string or Unix timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @return string Formatted date string
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function toAtom($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->toAtom($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::toRSS()
*
* @param string $dateString Datetime string or Unix timestamp
* @param integer $userOffset User's offset from GMT (in hours)
* @return string Formatted date string
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function toRSS($dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->toRSS($dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::timeAgoInWords()
*
2011-07-29 02:45:47 +00:00
* @param string $dateTime Datetime string or Unix timestamp
* @param array $options Default format if timestamp is used in $dateString
* @return string Relative time string.
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function timeAgoInWords($dateTime, $options = array()) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->timeAgoInWords($dateTime, $options);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::wasWithinLast()
*
* @param mixed $timeInterval the numeric value with space then time type.
* Example of valid types: 6 hours, 2 days, 1 minute.
* @param mixed $dateString the datestring or unix timestamp to compare
* @param integer $userOffset User's offset from GMT (in hours)
* @return boolean
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function wasWithinLast($timeInterval, $dateString, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->wasWithinLast($timeInterval, $dateString, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::gmt()
*
* @param string $string UNIX timestamp or a valid strtotime() date string
* @return integer UNIX timestamp
2011-10-16 13:36:51 +00:00
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function gmt($string = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->gmt($string);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::format()
*
2010-01-14 20:20:27 +00:00
* @param string $format date format string (or a DateTime string)
2011-07-29 02:45:47 +00:00
* @param string $date Datetime string (or a date format string)
* @param boolean $invalid flag to ignore results of fromString == false
2011-07-29 02:45:47 +00:00
* @param integer $userOffset User's offset from GMT (in hours)
* @return string Formatted date string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function format($format, $date = null, $invalid = false, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->format($format, $date, $invalid, $userOffset);
}
/**
2012-02-07 01:01:43 +00:00
* @see CakeTime::i18nFormat()
*
2011-07-29 02:45:47 +00:00
* @param string $date Datetime string
* @param string $format strftime format string.
* @param boolean $invalid flag to ignore results of fromString == false
* @param integer $userOffset User's offset from GMT (in hours)
* @return string Formatted and translated date string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) {
2012-02-07 01:01:43 +00:00
return $this->_CakeTime->i18nFormat($date, $format, $invalid, $userOffset);
}
}