TimeHelper.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 bool
  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. * @param string $method Method to call.
  112. * @param array $params Parameters to pass to method.
  113. * @return mixed Whatever is returned by called method, or false on failure
  114. */
  115. public function __call($method, $params) {
  116. return call_user_func_array(array($this->_engine, $method), $params);
  117. }
  118. /**
  119. * Converts a string representing the format for the function strftime and returns a
  120. * windows safe and i18n aware format.
  121. *
  122. * @param string $format Format with specifiers for strftime function.
  123. * Accepts the special specifier %S which mimics the modifier S for date()
  124. * @param string $time UNIX timestamp
  125. * @return string windows safe and date() function compatible format for strftime
  126. * @see CakeTime::convertSpecifiers()
  127. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  128. */
  129. public function convertSpecifiers($format, $time = null) {
  130. return $this->_engine->convertSpecifiers($format, $time);
  131. }
  132. /**
  133. * Converts given time (in server's time zone) to user's local time, given his/her timezone.
  134. *
  135. * @param string $serverTime UNIX timestamp
  136. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  137. * @return int UNIX timestamp
  138. * @see CakeTime::convert()
  139. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  140. */
  141. public function convert($serverTime, $timezone) {
  142. return $this->_engine->convert($serverTime, $timezone);
  143. }
  144. /**
  145. * Returns server's offset
  146. *
  147. * @return int Offset
  148. * @see CakeTime::serverOffset()
  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. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  158. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  159. * @return string Parsed timestamp
  160. * @see CakeTime::fromString()
  161. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  162. */
  163. public function fromString($dateString, $timezone = null) {
  164. return $this->_engine->fromString($dateString, $timezone);
  165. }
  166. /**
  167. * Returns a nicely formatted date string for given Datetime string.
  168. *
  169. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  170. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  171. * @param string $format The format to use. If null, `CakeTime::$niceFormat` is used
  172. * @return string Formatted date string
  173. * @see CakeTime::nice()
  174. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  175. */
  176. public function nice($dateString = null, $timezone = null, $format = null) {
  177. return $this->_engine->nice($dateString, $timezone, $format);
  178. }
  179. /**
  180. * Returns a formatted descriptive date string for given datetime string.
  181. *
  182. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime objectp
  183. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  184. * @return string Described, relative date string
  185. * @see CakeTime::niceShort()
  186. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  187. */
  188. public function niceShort($dateString = null, $timezone = null) {
  189. return $this->_engine->niceShort($dateString, $timezone);
  190. }
  191. /**
  192. * Returns a partial SQL string to search for all records between two dates.
  193. *
  194. * @param int|string|DateTime $begin UNIX timestamp, strtotime() valid string or DateTime object
  195. * @param int|string|DateTime $end UNIX timestamp, strtotime() valid string or DateTime object
  196. * @param string $fieldName Name of database field to compare with
  197. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  198. * @return string Partial SQL string.
  199. * @see CakeTime::daysAsSql()
  200. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  201. */
  202. public function daysAsSql($begin, $end, $fieldName, $timezone = null) {
  203. return $this->_engine->daysAsSql($begin, $end, $fieldName, $timezone);
  204. }
  205. /**
  206. * Returns a partial SQL string to search for all records between two times
  207. * occurring on the same day.
  208. *
  209. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  210. * @param string $fieldName Name of database field to compare with
  211. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  212. * @return string Partial SQL string.
  213. * @see CakeTime::dayAsSql()
  214. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  215. */
  216. public function dayAsSql($dateString, $fieldName, $timezone = null) {
  217. return $this->_engine->dayAsSql($dateString, $fieldName, $timezone);
  218. }
  219. /**
  220. * Returns true if given datetime string is today.
  221. *
  222. * @param int|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 bool True if datetime string is today
  225. * @see CakeTime::isToday()
  226. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  227. */
  228. public function isToday($dateString, $timezone = null) {
  229. return $this->_engine->isToday($dateString, $timezone);
  230. }
  231. /**
  232. * Returns true if given datetime string is within this week.
  233. *
  234. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  235. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  236. * @return bool True if datetime string is within current week
  237. * @see CakeTime::isThisWeek()
  238. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  239. */
  240. public function isThisWeek($dateString, $timezone = null) {
  241. return $this->_engine->isThisWeek($dateString, $timezone);
  242. }
  243. /**
  244. * Returns true if given datetime string is within this month
  245. *
  246. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  247. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  248. * @return bool True if datetime string is within current month
  249. * @see CakeTime::isThisMonth()
  250. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  251. */
  252. public function isThisMonth($dateString, $timezone = null) {
  253. return $this->_engine->isThisMonth($dateString, $timezone);
  254. }
  255. /**
  256. * Returns true if given datetime string is within current year.
  257. *
  258. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  259. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  260. * @return bool True if datetime string is within current year
  261. * @see CakeTime::isThisYear()
  262. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  263. */
  264. public function isThisYear($dateString, $timezone = null) {
  265. return $this->_engine->isThisYear($dateString, $timezone);
  266. }
  267. /**
  268. * Returns true if given datetime string was yesterday.
  269. *
  270. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  271. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  272. * @return bool True if datetime string was yesterday
  273. * @see CakeTime::wasYesterday()
  274. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  275. */
  276. public function wasYesterday($dateString, $timezone = null) {
  277. return $this->_engine->wasYesterday($dateString, $timezone);
  278. }
  279. /**
  280. * Returns true if given datetime string is tomorrow.
  281. *
  282. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  283. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  284. * @return bool True if datetime string was yesterday
  285. * @see CakeTime::isTomorrow()
  286. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  287. */
  288. public function isTomorrow($dateString, $timezone = null) {
  289. return $this->_engine->isTomorrow($dateString, $timezone);
  290. }
  291. /**
  292. * Returns the quarter
  293. *
  294. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  295. * @param bool $range if true returns a range in Y-m-d format
  296. * @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
  297. * @see CakeTime::toQuarter()
  298. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  299. */
  300. public function toQuarter($dateString, $range = false) {
  301. return $this->_engine->toQuarter($dateString, $range);
  302. }
  303. /**
  304. * Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
  305. *
  306. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  307. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  308. * @return int Unix timestamp
  309. * @see CakeTime::toUnix()
  310. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  311. */
  312. public function toUnix($dateString, $timezone = null) {
  313. return $this->_engine->toUnix($dateString, $timezone);
  314. }
  315. /**
  316. * Returns a date formatted for Atom RSS feeds.
  317. *
  318. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  319. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  320. * @return string Formatted date string
  321. * @see CakeTime::toAtom()
  322. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  323. */
  324. public function toAtom($dateString, $timezone = null) {
  325. return $this->_engine->toAtom($dateString, $timezone);
  326. }
  327. /**
  328. * Formats date for RSS feeds
  329. *
  330. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  331. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  332. * @return string Formatted date string
  333. * @see CakeTime::toRSS()
  334. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  335. */
  336. public function toRSS($dateString, $timezone = null) {
  337. return $this->_engine->toRSS($dateString, $timezone);
  338. }
  339. /**
  340. * Formats a date into a phrase expressing the relative time.
  341. *
  342. * ## Addition options
  343. *
  344. * - `element` - The element to wrap the formatted time in.
  345. * Has a few additional options:
  346. * - `tag` - The tag to use, defaults to 'span'.
  347. * - `class` - The class name to use, defaults to `time-ago-in-words`.
  348. * - `title` - Defaults to the $dateTime input.
  349. *
  350. * @param int|string|DateTime $dateTime UNIX timestamp, strtotime() valid string or DateTime object
  351. * @param array $options Default format if timestamp is used in $dateString
  352. * @return string Relative time string.
  353. * @see CakeTime::timeAgoInWords()
  354. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  355. */
  356. public function timeAgoInWords($dateTime, $options = array()) {
  357. $element = null;
  358. if (!empty($options['element'])) {
  359. $element = array(
  360. 'tag' => 'span',
  361. 'class' => 'time-ago-in-words',
  362. 'title' => $dateTime
  363. );
  364. if (is_array($options['element'])) {
  365. $element = $options['element'] + $element;
  366. } else {
  367. $element['tag'] = $options['element'];
  368. }
  369. unset($options['element']);
  370. }
  371. $relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
  372. if ($element) {
  373. $relativeDate = sprintf(
  374. '<%s%s>%s</%s>',
  375. $element['tag'],
  376. $this->_parseAttributes($element, array('tag')),
  377. $relativeDate,
  378. $element['tag']
  379. );
  380. }
  381. return $relativeDate;
  382. }
  383. /**
  384. * Returns true if specified datetime was within the interval specified, else false.
  385. *
  386. * @param string|int $timeInterval the numeric value with space then time type.
  387. * Example of valid types: 6 hours, 2 days, 1 minute.
  388. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  389. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  390. * @return bool
  391. * @see CakeTime::wasWithinLast()
  392. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  393. */
  394. public function wasWithinLast($timeInterval, $dateString, $timezone = null) {
  395. return $this->_engine->wasWithinLast($timeInterval, $dateString, $timezone);
  396. }
  397. /**
  398. * Returns true if specified datetime is within the interval specified, else false.
  399. *
  400. * @param string|int $timeInterval the numeric value with space then time type.
  401. * Example of valid types: 6 hours, 2 days, 1 minute.
  402. * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  403. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  404. * @return bool
  405. * @see CakeTime::isWithinLast()
  406. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  407. */
  408. public function isWithinNext($timeInterval, $dateString, $timezone = null) {
  409. return $this->_engine->isWithinNext($timeInterval, $dateString, $timezone);
  410. }
  411. /**
  412. * Returns gmt as a UNIX timestamp.
  413. *
  414. * @param int|string|DateTime $string UNIX timestamp, strtotime() valid string or DateTime object
  415. * @return int UNIX timestamp
  416. * @see CakeTime::gmt()
  417. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  418. */
  419. public function gmt($string = null) {
  420. return $this->_engine->gmt($string);
  421. }
  422. /**
  423. * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  424. * This function also accepts a time string and a format string as first and second parameters.
  425. * In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
  426. *
  427. * ## Examples
  428. *
  429. * Create localized & formatted time:
  430. *
  431. * {{{
  432. * $this->Time->format('2012-02-15', '%m-%d-%Y'); // returns 02-15-2012
  433. * $this->Time->format('2012-02-15 23:01:01', '%c'); // returns preferred date and time based on configured locale
  434. * $this->Time->format('0000-00-00', '%d-%m-%Y', 'N/A'); // return N/A becuase an invalid date was passed
  435. * $this->Time->format('2012-02-15 23:01:01', '%c', 'N/A', 'America/New_York'); // converts passed date to timezone
  436. * }}}
  437. *
  438. * @param int|string|DateTime $format date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
  439. * @param int|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
  440. * @param bool $invalid flag to ignore results of fromString == false
  441. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  442. * @return string Formatted date string
  443. * @see CakeTime::format()
  444. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  445. */
  446. public function format($format, $date = null, $invalid = false, $timezone = null) {
  447. return $this->_engine->format($format, $date, $invalid, $timezone);
  448. }
  449. /**
  450. * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  451. * It takes into account the default date format for the current language if a LC_TIME file is used.
  452. *
  453. * @param int|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
  454. * @param string $format strftime format string.
  455. * @param bool $invalid flag to ignore results of fromString == false
  456. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
  457. * @return string Formatted and translated date string
  458. * @see CakeTime::i18nFormat()
  459. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  460. */
  461. public function i18nFormat($date, $format = null, $invalid = false, $timezone = null) {
  462. return $this->_engine->i18nFormat($date, $format, $invalid, $timezone);
  463. }
  464. }