TimeHelper.php 16 KB

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