TimeHelper.php 16 KB

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