TimeHelper.php 16 KB

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