TimeHelper.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. /**
  3. * Time Helper class file.
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.View.Helper
  15. * @since CakePHP(tm) v 0.10.0.1076
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('CakeTime', 'Utility');
  19. App::uses('Multibyte', 'I18n');
  20. App::uses('AppHelper', 'View/Helper');
  21. /**
  22. * Time Helper class for easy use of time data.
  23. *
  24. * Manipulation of time data.
  25. *
  26. * @package Cake.View.Helper
  27. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
  28. * @see CakeTime
  29. */
  30. class TimeHelper extends AppHelper {
  31. /**
  32. * CakeTime instance
  33. *
  34. * @var stdClass
  35. */
  36. protected $_engine = null;
  37. /**
  38. * Constructor
  39. *
  40. * ### Settings:
  41. *
  42. * - `engine` Class name to use to replace CakeTime functionality
  43. * The class needs to be placed in the `Utility` directory.
  44. *
  45. * @param View $View the view object the helper is attached to.
  46. * @param array $settings Settings array
  47. * @throws CakeException When the engine class could not be found.
  48. */
  49. public function __construct(View $View, $settings = array()) {
  50. $settings = Hash::merge(array('engine' => 'CakeTime'), $settings);
  51. parent::__construct($View, $settings);
  52. list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
  53. App::uses($engineClass, $plugin . 'Utility');
  54. if (class_exists($engineClass)) {
  55. $this->_engine = new $engineClass($settings);
  56. } else {
  57. throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
  58. }
  59. }
  60. /**
  61. * Magic accessor for deprecated attributes.
  62. *
  63. * @param string $name Name of the attribute to set.
  64. * @param string $value Value of the attribute to set.
  65. * @return void
  66. */
  67. public function __set($name, $value) {
  68. switch ($name) {
  69. case 'niceFormat':
  70. $this->_engine->{$name} = $value;
  71. break;
  72. default:
  73. $this->{$name} = $value;
  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. * @return mixed Whatever is returned by called method, or false on failure
  111. */
  112. public function __call($method, $params) {
  113. return call_user_func_array(array($this->_engine, $method), $params);
  114. }
  115. /**
  116. * Converts a string representing the format for the function strftime and returns a
  117. * windows safe and i18n aware format.
  118. *
  119. * @see CakeTime::convertSpecifiers()
  120. *
  121. * @param string $format Format with specifiers for strftime function.
  122. * Accepts the special specifier %S which mimics the modifier S for date()
  123. * @param string $time UNIX timestamp
  124. * @return string windows safe and date() function compatible format for strftime
  125. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  126. */
  127. public function convertSpecifiers($format, $time = null) {
  128. return $this->_engine->convertSpecifiers($format, $time);
  129. }
  130. /**
  131. * Converts given time (in server's time zone) to user's local time, given his/her timezone.
  132. *
  133. * @see CakeTime::convert()
  134. *
  135. * @param string $serverTime UNIX timestamp
  136. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  137. * @return integer UNIX timestamp
  138. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  139. */
  140. public function convert($serverTime, $timezone) {
  141. return $this->_engine->convert($serverTime, $timezone);
  142. }
  143. /**
  144. * Returns server's offset
  145. *
  146. * @see CakeTime::serverOffset()
  147. *
  148. * @return integer Offset
  149. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  150. */
  151. public function serverOffset() {
  152. return $this->_engine->serverOffset();
  153. }
  154. /**
  155. * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
  156. *
  157. * @see CakeTime::fromString()
  158. *
  159. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  160. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  161. * @return string Parsed timestamp
  162. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  163. */
  164. public function fromString($dateString, $timezone = null) {
  165. return $this->_engine->fromString($dateString, $timezone);
  166. }
  167. /**
  168. * Returns a nicely formatted date string for given Datetime string.
  169. *
  170. * @see CakeTime::nice()
  171. *
  172. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  173. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  174. * @param string $format The format to use. If null, `CakeTime::$niceFormat` is used
  175. * @return string Formatted date string
  176. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  177. */
  178. public function nice($dateString = null, $timezone = null, $format = null) {
  179. return $this->_engine->nice($dateString, $timezone, $format);
  180. }
  181. /**
  182. * Returns a formatted descriptive date string for given datetime string.
  183. *
  184. * @see CakeTime::niceShort()
  185. *
  186. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime objectp
  187. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  188. * @return string Described, relative date string
  189. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  190. */
  191. public function niceShort($dateString = null, $timezone = null) {
  192. return $this->_engine->niceShort($dateString, $timezone);
  193. }
  194. /**
  195. * Returns a partial SQL string to search for all records between two dates.
  196. *
  197. * @see CakeTime::daysAsSql()
  198. *
  199. * @param integer|string|DateTime $begin UNIX timestamp, strtotime() valid string or DateTime object
  200. * @param integer|string|DateTime $end UNIX timestamp, strtotime() valid string or DateTime object
  201. * @param string $fieldName Name of database field to compare with
  202. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  203. * @return string Partial SQL string.
  204. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  205. */
  206. public function daysAsSql($begin, $end, $fieldName, $timezone = null) {
  207. return $this->_engine->daysAsSql($begin, $end, $fieldName, $timezone);
  208. }
  209. /**
  210. * Returns a partial SQL string to search for all records between two times
  211. * occurring on the same day.
  212. *
  213. * @see CakeTime::dayAsSql()
  214. *
  215. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  216. * @param string $fieldName Name of database field to compare with
  217. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  218. * @return string Partial SQL string.
  219. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  220. */
  221. public function dayAsSql($dateString, $fieldName, $timezone = null) {
  222. return $this->_engine->dayAsSql($dateString, $fieldName, $timezone);
  223. }
  224. /**
  225. * Returns true if given datetime string is today.
  226. *
  227. * @see CakeTime::isToday()
  228. *
  229. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  230. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  231. * @return boolean True if datetime string is today
  232. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  233. */
  234. public function isToday($dateString, $timezone = null) {
  235. return $this->_engine->isToday($dateString, $timezone);
  236. }
  237. /**
  238. * Returns true if given datetime string is within this week.
  239. *
  240. * @see CakeTime::isThisWeek()
  241. *
  242. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  243. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  244. * @return boolean True if datetime string is within current week
  245. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  246. */
  247. public function isThisWeek($dateString, $timezone = null) {
  248. return $this->_engine->isThisWeek($dateString, $timezone);
  249. }
  250. /**
  251. * Returns true if given datetime string is within this month
  252. *
  253. * @see CakeTime::isThisMonth()
  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 is within current month
  258. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  259. */
  260. public function isThisMonth($dateString, $timezone = null) {
  261. return $this->_engine->isThisMonth($dateString, $timezone);
  262. }
  263. /**
  264. * Returns true if given datetime string is within current year.
  265. *
  266. * @see CakeTime::isThisYear()
  267. *
  268. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  269. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  270. * @return boolean True if datetime string is within current year
  271. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  272. */
  273. public function isThisYear($dateString, $timezone = null) {
  274. return $this->_engine->isThisYear($dateString, $timezone);
  275. }
  276. /**
  277. * Returns true if given datetime string was yesterday.
  278. *
  279. * @see CakeTime::wasYesterday()
  280. *
  281. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  282. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  283. * @return boolean True if datetime string was yesterday
  284. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  285. *
  286. */
  287. public function wasYesterday($dateString, $timezone = null) {
  288. return $this->_engine->wasYesterday($dateString, $timezone);
  289. }
  290. /**
  291. * Returns true if given datetime string is tomorrow.
  292. *
  293. * @see CakeTime::isTomorrow()
  294. *
  295. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  296. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  297. * @return boolean True if datetime string was yesterday
  298. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  299. */
  300. public function isTomorrow($dateString, $timezone = null) {
  301. return $this->_engine->isTomorrow($dateString, $timezone);
  302. }
  303. /**
  304. * Returns the quarter
  305. *
  306. * @see CakeTime::toQuarter()
  307. *
  308. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  309. * @param boolean $range if true returns a range in Y-m-d format
  310. * @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
  311. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  312. */
  313. public function toQuarter($dateString, $range = false) {
  314. return $this->_engine->toQuarter($dateString, $range);
  315. }
  316. /**
  317. * Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
  318. *
  319. * @see CakeTime::toUnix()
  320. *
  321. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  322. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  323. * @return integer Unix timestamp
  324. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  325. */
  326. public function toUnix($dateString, $timezone = null) {
  327. return $this->_engine->toUnix($dateString, $timezone);
  328. }
  329. /**
  330. * Returns a date formatted for Atom RSS feeds.
  331. *
  332. * @see CakeTime::toAtom()
  333. *
  334. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  335. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  336. * @return string Formatted date string
  337. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  338. */
  339. public function toAtom($dateString, $timezone = null) {
  340. return $this->_engine->toAtom($dateString, $timezone);
  341. }
  342. /**
  343. * Formats date for RSS feeds
  344. *
  345. * @see CakeTime::toRSS()
  346. *
  347. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  348. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  349. * @return string Formatted date string
  350. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  351. */
  352. public function toRSS($dateString, $timezone = null) {
  353. return $this->_engine->toRSS($dateString, $timezone);
  354. }
  355. /**
  356. * Formats date for RSS feeds
  357. *
  358. * @see CakeTime::timeAgoInWords()
  359. *
  360. * ## Addition options
  361. *
  362. * - `element` - The element to wrap the formatted time in.
  363. * Has a few additional options:
  364. * - `tag` - The tag to use, defaults to 'span'.
  365. * - `class` - The class name to use, defaults to `time-ago-in-words`.
  366. * - `title` - Defaults to the $dateTime input.
  367. *
  368. * @param integer|string|DateTime $dateTime UNIX timestamp, strtotime() valid string or DateTime object
  369. * @param array $options Default format if timestamp is used in $dateString
  370. * @return string Relative time string.
  371. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  372. */
  373. public function timeAgoInWords($dateTime, $options = array()) {
  374. $element = null;
  375. if (!empty($options['element'])) {
  376. $element = array(
  377. 'tag' => 'span',
  378. 'class' => 'time-ago-in-words',
  379. 'title' => $dateTime
  380. );
  381. if (is_array($options['element'])) {
  382. $element = $options['element'] + $element;
  383. } else {
  384. $element['tag'] = $options['element'];
  385. }
  386. unset($options['element']);
  387. }
  388. $relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
  389. if ($element) {
  390. $relativeDate = sprintf(
  391. '<%s%s>%s</%s>',
  392. $element['tag'],
  393. $this->_parseAttributes($element, array('tag')),
  394. $relativeDate,
  395. $element['tag']
  396. );
  397. }
  398. return $relativeDate;
  399. }
  400. /**
  401. * Returns true if specified datetime was within the interval specified, else false.
  402. *
  403. * @see CakeTime::wasWithinLast()
  404. *
  405. * @param string|integer $timeInterval the numeric value with space then time type.
  406. * Example of valid types: 6 hours, 2 days, 1 minute.
  407. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  408. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  409. * @return boolean
  410. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  411. */
  412. public function wasWithinLast($timeInterval, $dateString, $timezone = null) {
  413. return $this->_engine->wasWithinLast($timeInterval, $dateString, $timezone);
  414. }
  415. /**
  416. * Returns true if specified datetime is within the interval specified, else false.
  417. *
  418. * @see CakeTime::isWithinLast()
  419. *
  420. * @param string|integer $timeInterval the numeric value with space then time type.
  421. * Example of valid types: 6 hours, 2 days, 1 minute.
  422. * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  423. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  424. * @return boolean
  425. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  426. */
  427. public function isWithinNext($timeInterval, $dateString, $timezone = null) {
  428. return $this->_engine->isWithinNext($timeInterval, $dateString, $timezone);
  429. }
  430. /**
  431. * Returns gmt as a UNIX timestamp.
  432. *
  433. * @see CakeTime::gmt()
  434. *
  435. * @param integer|string|DateTime $string UNIX timestamp, strtotime() valid string or DateTime object
  436. * @return integer UNIX timestamp
  437. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  438. */
  439. public function gmt($string = null) {
  440. return $this->_engine->gmt($string);
  441. }
  442. /**
  443. * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  444. * This function also accepts a time string and a format string as first and second parameters.
  445. * In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
  446. *
  447. * ## Examples
  448. *
  449. * Create localized & formatted time:
  450. *
  451. * {{{
  452. * $this->Time->format('2012-02-15', '%m-%d-%Y'); // returns 02-15-2012
  453. * $this->Time->format('2012-02-15 23:01:01', '%c'); // returns preferred date and time based on configured locale
  454. * $this->Time->format('0000-00-00', '%d-%m-%Y', 'N/A'); // return N/A becuase an invalid date was passed
  455. * $this->Time->format('2012-02-15 23:01:01', '%c', 'N/A', 'America/New_York'); // converts passed date to timezone
  456. * }}}
  457. *
  458. * @see CakeTime::format()
  459. *
  460. * @param integer|string|DateTime $format date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
  461. * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
  462. * @param boolean $invalid flag to ignore results of fromString == false
  463. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  464. * @return string Formatted date string
  465. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  466. */
  467. public function format($format, $date = null, $invalid = false, $timezone = null) {
  468. return $this->_engine->format($format, $date, $invalid, $timezone);
  469. }
  470. /**
  471. * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  472. * It takes into account the default date format for the current language if a LC_TIME file is used.
  473. *
  474. * @see CakeTime::i18nFormat()
  475. *
  476. * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
  477. * @param string $format strftime format string.
  478. * @param boolean $invalid flag to ignore results of fromString == false
  479. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  480. * @return string Formatted and translated date string
  481. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  482. */
  483. public function i18nFormat($date, $format = null, $invalid = false, $timezone = null) {
  484. return $this->_engine->i18nFormat($date, $format, $invalid, $timezone);
  485. }
  486. }