TimeHelper.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * Time Helper class file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.View.Helper
  16. * @since CakePHP(tm) v 0.10.0.1076
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeTime', 'Utility');
  20. App::uses('Multibyte', 'I18n');
  21. App::uses('AppHelper', 'View/Helper');
  22. /**
  23. * Time Helper class for easy use of time data.
  24. *
  25. * Manipulation of time data.
  26. *
  27. * @package Cake.View.Helper
  28. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
  29. * @see CakeTime
  30. */
  31. class TimeHelper extends AppHelper {
  32. /**
  33. * CakeTime instance
  34. */
  35. protected $_CakeTime = null;
  36. /**
  37. * Constructor
  38. *
  39. * @param View $View the view object the helper is attached to.
  40. * @param array $settings Settings array Settings array
  41. */
  42. public function __construct(View $View, $settings = array()) {
  43. $settings = Set::merge(array('engine' => 'CakeTime'), $settings);
  44. parent::__construct($View, $settings);
  45. $engineClass = $settings['engine'];
  46. App::uses($engineClass, 'Utility');
  47. if (class_exists($engineClass)) {
  48. $this->_CakeTime = new $engineClass($settings);
  49. } else {
  50. throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
  51. }
  52. }
  53. /**
  54. * Magic accessor for deprecated attributes.
  55. *
  56. * @param string $name Name of the attribute to set.
  57. * @param string $value Value of the attribute to set.
  58. * @return mixed
  59. */
  60. public function __set($name, $value) {
  61. switch ($name) {
  62. case 'niceFormat':
  63. $this->_CakeTime->{$name} = $value;
  64. break;
  65. default:
  66. $this->{$name} = $value;
  67. break;
  68. }
  69. }
  70. /**
  71. * Magic isset check for deprecated attributes.
  72. *
  73. * @param string $name Name of the attribute to check.
  74. * @return boolean
  75. */
  76. public function __isset($name) {
  77. if (isset($this->{$name})) {
  78. return true;
  79. }
  80. $magicGet = array('niceFormat');
  81. if (in_array($name, $magicGet)) {
  82. return $this->__get($name) !== null;
  83. }
  84. return null;
  85. }
  86. /**
  87. * Magic accessor for attributes that were deprecated.
  88. *
  89. * @param string $name Name of the attribute to get.
  90. * @return mixed
  91. */
  92. public function __get($name) {
  93. if (isset($this->_CakeTime->{$name})) {
  94. return $this->_CakeTime->{$name};
  95. }
  96. $magicGet = array('niceFormat');
  97. if (in_array($name, $magicGet)) {
  98. return $this->_CakeTime->{$name};
  99. }
  100. return null;
  101. }
  102. /**
  103. * Call methods from CakeTime utility class
  104. */
  105. public function __call($method, $params) {
  106. return call_user_func_array(array($this->_CakeTime, $method), $params);
  107. }
  108. /**
  109. * @see CakeTime::convertSpecifiers()
  110. *
  111. * @param string $format Format with specifiers for strftime function.
  112. * Accepts the special specifier %S which mimics the modifier S for date()
  113. * @param string $time UNIX timestamp
  114. * @return string windows safe and date() function compatible format for strftime
  115. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  116. */
  117. public function convertSpecifiers($format, $time = null) {
  118. return $this->_CakeTime->convertSpecifiers($format, $time);
  119. }
  120. /**
  121. * @see CakeTime::convert()
  122. *
  123. * @param string $serverTime UNIX timestamp
  124. * @param integer $userOffset User's offset from GMT (in hours)
  125. * @return integer UNIX timestamp
  126. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  127. */
  128. public function convert($serverTime, $userOffset) {
  129. return $this->_CakeTime->convert($serverTime, $userOffset);
  130. }
  131. /**
  132. * @see CakeTime::serverOffset()
  133. *
  134. * @return integer Offset
  135. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  136. */
  137. public function serverOffset() {
  138. return $this->_CakeTime->serverOffset();
  139. }
  140. /**
  141. * @see CakeTime::fromString()
  142. *
  143. * @param string $dateString Datetime string
  144. * @param integer $userOffset User's offset from GMT (in hours)
  145. * @return string Parsed timestamp
  146. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  147. */
  148. public function fromString($dateString, $userOffset = null) {
  149. return $this->_CakeTime->fromString($dateString, $userOffset);
  150. }
  151. /**
  152. * @see CakeTime::nice()
  153. *
  154. * @param string $dateString Datetime string or Unix timestamp
  155. * @param integer $userOffset User's offset from GMT (in hours)
  156. * @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used
  157. * @return string Formatted date string
  158. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  159. */
  160. public function nice($dateString = null, $userOffset = null, $format = null) {
  161. return $this->_CakeTime->nice($dateString, $userOffset, $format);
  162. }
  163. /**
  164. * @see CakeTime::niceShort()
  165. *
  166. * @param string $dateString Datetime string or Unix timestamp
  167. * @param integer $userOffset User's offset from GMT (in hours)
  168. * @return string Described, relative date string
  169. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  170. */
  171. public function niceShort($dateString = null, $userOffset = null) {
  172. return $this->_CakeTime->niceShort($dateString, $userOffset);
  173. }
  174. /**
  175. * @see CakeTime::daysAsSql()
  176. *
  177. * @param string $begin Datetime string or Unix timestamp
  178. * @param string $end Datetime string or Unix timestamp
  179. * @param string $fieldName Name of database field to compare with
  180. * @param integer $userOffset User's offset from GMT (in hours)
  181. * @return string Partial SQL string.
  182. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  183. */
  184. public function daysAsSql($begin, $end, $fieldName, $userOffset = null) {
  185. return $this->_CakeTime->daysAsSql($begin, $end, $fieldName, $userOffset);
  186. }
  187. /**
  188. * @see CakeTime::dayAsSql()
  189. *
  190. * @param string $dateString Datetime string or Unix timestamp
  191. * @param string $fieldName Name of database field to compare with
  192. * @param integer $userOffset User's offset from GMT (in hours)
  193. * @return string Partial SQL string.
  194. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  195. */
  196. public function dayAsSql($dateString, $fieldName, $userOffset = null) {
  197. return $this->_CakeTime->dayAsSql($dateString, $fieldName, $userOffset);
  198. }
  199. /**
  200. * @see CakeTime::isToday()
  201. *
  202. * @param string $dateString Datetime string or Unix timestamp
  203. * @param integer $userOffset User's offset from GMT (in hours)
  204. * @return boolean True if datetime string is today
  205. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  206. */
  207. public function isToday($dateString, $userOffset = null) {
  208. return $this->_CakeTime->isToday($dateString, $userOffset);
  209. }
  210. /**
  211. * @see CakeTime::isThisWeek()
  212. *
  213. * @param string $dateString
  214. * @param integer $userOffset User's offset from GMT (in hours)
  215. * @return boolean True if datetime string is within current week
  216. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  217. */
  218. public function isThisWeek($dateString, $userOffset = null) {
  219. return $this->_CakeTime->isThisWeek($dateString, $userOffset);
  220. }
  221. /**
  222. * @see CakeTime::isThisMonth()
  223. *
  224. * @param string $dateString
  225. * @param integer $userOffset User's offset from GMT (in hours)
  226. * @return boolean True if datetime string is within current month
  227. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  228. */
  229. public function isThisMonth($dateString, $userOffset = null) {
  230. return $this->_CakeTime->isThisMonth($dateString, $userOffset);
  231. }
  232. /**
  233. * @see CakeTime::isThisYear()
  234. *
  235. * @param string $dateString Datetime string or Unix timestamp
  236. * @param integer $userOffset User's offset from GMT (in hours)
  237. * @return boolean True if datetime string is within current year
  238. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  239. */
  240. public function isThisYear($dateString, $userOffset = null) {
  241. return $this->_CakeTime->isThisYear($dateString, $userOffset);
  242. }
  243. /**
  244. * @see CakeTime::wasYesterday()
  245. *
  246. * @param string $dateString Datetime string or Unix timestamp
  247. * @param integer $userOffset User's offset from GMT (in hours)
  248. * @return boolean True if datetime string was yesterday
  249. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  250. *
  251. */
  252. public function wasYesterday($dateString, $userOffset = null) {
  253. return $this->_CakeTime->wasYesterday($dateString, $userOffset);
  254. }
  255. /**
  256. * @see CakeTime::isTomorrow()
  257. *
  258. * @param string $dateString Datetime string or Unix timestamp
  259. * @param integer $userOffset User's offset from GMT (in hours)
  260. * @return boolean True if datetime string was yesterday
  261. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  262. */
  263. public function isTomorrow($dateString, $userOffset = null) {
  264. return $this->_CakeTime->isTomorrow($dateString, $userOffset);
  265. }
  266. /**
  267. * @see CakeTime::toQuarter()
  268. *
  269. * @param string $dateString
  270. * @param boolean $range if true returns a range in Y-m-d format
  271. * @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
  272. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  273. */
  274. public function toQuarter($dateString, $range = false) {
  275. return $this->_CakeTime->toQuarter($dateString, $range);
  276. }
  277. /**
  278. * @see CakeTime::toUnix()
  279. *
  280. * @param string $dateString Datetime string to be represented as a Unix timestamp
  281. * @param integer $userOffset User's offset from GMT (in hours)
  282. * @return integer Unix timestamp
  283. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  284. */
  285. public function toUnix($dateString, $userOffset = null) {
  286. return $this->_CakeTime->toUnix($dateString, $userOffset);
  287. }
  288. /**
  289. * @see CakeTime::toAtom()
  290. *
  291. * @param string $dateString Datetime string or Unix timestamp
  292. * @param integer $userOffset User's offset from GMT (in hours)
  293. * @return string Formatted date string
  294. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  295. */
  296. public function toAtom($dateString, $userOffset = null) {
  297. return $this->_CakeTime->toAtom($dateString, $userOffset);
  298. }
  299. /**
  300. * @see CakeTime::toRSS()
  301. *
  302. * @param string $dateString Datetime string or Unix timestamp
  303. * @param integer $userOffset User's offset from GMT (in hours)
  304. * @return string Formatted date string
  305. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  306. */
  307. public function toRSS($dateString, $userOffset = null) {
  308. return $this->_CakeTime->toRSS($dateString, $userOffset);
  309. }
  310. /**
  311. * @see CakeTime::timeAgoInWords()
  312. *
  313. * @param string $dateTime Datetime string or Unix timestamp
  314. * @param array $options Default format if timestamp is used in $dateString
  315. * @return string Relative time string.
  316. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  317. */
  318. public function timeAgoInWords($dateTime, $options = array()) {
  319. return $this->_CakeTime->timeAgoInWords($dateTime, $options);
  320. }
  321. /**
  322. * @see CakeTime::wasWithinLast()
  323. *
  324. * @param mixed $timeInterval the numeric value with space then time type.
  325. * Example of valid types: 6 hours, 2 days, 1 minute.
  326. * @param mixed $dateString the datestring or unix timestamp to compare
  327. * @param integer $userOffset User's offset from GMT (in hours)
  328. * @return boolean
  329. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  330. */
  331. public function wasWithinLast($timeInterval, $dateString, $userOffset = null) {
  332. return $this->_CakeTime->wasWithinLast($timeInterval, $dateString, $userOffset);
  333. }
  334. /**
  335. * @see CakeTime::gmt()
  336. *
  337. * @param string $string UNIX timestamp or a valid strtotime() date string
  338. * @return integer UNIX timestamp
  339. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  340. */
  341. public function gmt($string = null) {
  342. return $this->_CakeTime->gmt($string);
  343. }
  344. /**
  345. * @see CakeTime::format()
  346. *
  347. * @param string $format date format string (or a DateTime string)
  348. * @param string $date Datetime string (or a date format string)
  349. * @param boolean $invalid flag to ignore results of fromString == false
  350. * @param integer $userOffset User's offset from GMT (in hours)
  351. * @return string Formatted date string
  352. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  353. */
  354. public function format($format, $date = null, $invalid = false, $userOffset = null) {
  355. return $this->_CakeTime->format($format, $date, $invalid, $userOffset);
  356. }
  357. /**
  358. * @see CakeTime::i18nFormat()
  359. *
  360. * @param string $date Datetime string
  361. * @param string $format strftime format string.
  362. * @param boolean $invalid flag to ignore results of fromString == false
  363. * @param integer $userOffset User's offset from GMT (in hours)
  364. * @return string Formatted and translated date string
  365. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  366. */
  367. public function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) {
  368. return $this->_CakeTime->i18nFormat($date, $format, $invalid, $userOffset);
  369. }
  370. }