PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/view/helpers/time.php

https://github.com/hack521/contenidopago
PHP | 755 lines | 408 code | 59 blank | 288 comment | 95 complexity | 93088b5c2091480375ac8c5f0d9898e8 MD5 | raw file
  1. <?php
  2. /**
  3. * Time Helper class file.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.libs.view.helpers
  17. * @since CakePHP(tm) v 0.10.0.1076
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Time Helper class for easy use of time data.
  22. *
  23. * Manipulation of time data.
  24. *
  25. * @package cake
  26. * @subpackage cake.cake.libs.view.helpers
  27. * @link http://book.cakephp.org/view/1470/Time
  28. */
  29. class TimeHelper extends AppHelper {
  30. /**
  31. * Converts a string representing the format for the function strftime and returns a
  32. * windows safe and i18n aware format.
  33. *
  34. * @param string $format Format with specifiers for strftime function.
  35. * Accepts the special specifier %S which mimics th modifier S for date()
  36. * @param string UNIX timestamp
  37. * @return string windows safe and date() function compatible format for strftime
  38. * @access public
  39. */
  40. function convertSpecifiers($format, $time = null) {
  41. if (!$time) {
  42. $time = time();
  43. }
  44. $this->__time = $time;
  45. return preg_replace_callback('/\%(\w+)/', array($this, '__translateSpecifier'), $format);
  46. }
  47. /**
  48. * Auxiliary function to translate a matched specifier element from a regular expresion into
  49. * a windows safe and i18n aware specifier
  50. *
  51. * @param array $specifier match from regular expression
  52. * @return string converted element
  53. * @access private
  54. */
  55. function __translateSpecifier($specifier) {
  56. switch ($specifier[1]) {
  57. case 'a':
  58. $abday = __c('abday', 5, true);
  59. if (is_array($abday)) {
  60. return $abday[date('w', $this->__time)];
  61. }
  62. break;
  63. case 'A':
  64. $day = __c('day',5,true);
  65. if (is_array($day)) {
  66. return $day[date('w', $this->__time)];
  67. }
  68. break;
  69. case 'c':
  70. $format = __c('d_t_fmt',5,true);
  71. if ($format != 'd_t_fmt') {
  72. return $this->convertSpecifiers($format, $this->__time);
  73. }
  74. break;
  75. case 'C':
  76. return sprintf("%02d", date('Y', $this->__time) / 100);
  77. case 'D':
  78. return '%m/%d/%y';
  79. case 'e':
  80. if (DS === '/') {
  81. return '%e';
  82. }
  83. $day = date('j', $this->__time);
  84. if ($day < 10) {
  85. $day = ' ' . $day;
  86. }
  87. return $day;
  88. case 'eS' :
  89. return date('jS', $this->__time);
  90. case 'b':
  91. case 'h':
  92. $months = __c('abmon', 5, true);
  93. if (is_array($months)) {
  94. return $months[date('n', $this->__time) -1];
  95. }
  96. return '%b';
  97. case 'B':
  98. $months = __c('mon',5,true);
  99. if (is_array($months)) {
  100. return $months[date('n', $this->__time) -1];
  101. }
  102. break;
  103. case 'n':
  104. return "\n";
  105. case 'p':
  106. case 'P':
  107. $default = array('am' => 0, 'pm' => 1);
  108. $meridiem = $default[date('a',$this->__time)];
  109. $format = __c('am_pm', 5, true);
  110. if (is_array($format)) {
  111. $meridiem = $format[$meridiem];
  112. return ($specifier[1] == 'P') ? strtolower($meridiem) : strtoupper($meridiem);
  113. }
  114. break;
  115. case 'r':
  116. $complete = __c('t_fmt_ampm', 5, true);
  117. if ($complete != 't_fmt_ampm') {
  118. return str_replace('%p',$this->__translateSpecifier(array('%p', 'p')),$complete);
  119. }
  120. break;
  121. case 'R':
  122. return date('H:i', $this->__time);
  123. case 't':
  124. return "\t";
  125. case 'T':
  126. return '%H:%M:%S';
  127. case 'u':
  128. return ($weekDay = date('w', $this->__time)) ? $weekDay : 7;
  129. case 'x':
  130. $format = __c('d_fmt', 5, true);
  131. if ($format != 'd_fmt') {
  132. return $this->convertSpecifiers($format, $this->__time);
  133. }
  134. break;
  135. case 'X':
  136. $format = __c('t_fmt',5,true);
  137. if ($format != 't_fmt') {
  138. return $this->convertSpecifiers($format, $this->__time);
  139. }
  140. break;
  141. }
  142. return $specifier[0];
  143. }
  144. /**
  145. * Converts given time (in server's time zone) to user's local time, given his/her offset from GMT.
  146. *
  147. * @param string $serverTime UNIX timestamp
  148. * @param int $userOffset User's offset from GMT (in hours)
  149. * @return string UNIX timestamp
  150. * @access public
  151. */
  152. function convert($serverTime, $userOffset) {
  153. $serverOffset = $this->serverOffset();
  154. $gmtTime = $serverTime - $serverOffset;
  155. $userTime = $gmtTime + $userOffset * (60*60);
  156. return $userTime;
  157. }
  158. /**
  159. * Returns server's offset from GMT in seconds.
  160. *
  161. * @return int Offset
  162. * @access public
  163. */
  164. function serverOffset() {
  165. return date('Z', time());
  166. }
  167. /**
  168. * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
  169. *
  170. * @param string $dateString Datetime string
  171. * @param int $userOffset User's offset from GMT (in hours)
  172. * @return string Parsed timestamp
  173. * @access public
  174. * @link http://book.cakephp.org/view/1471/Formatting
  175. */
  176. function fromString($dateString, $userOffset = null) {
  177. if (empty($dateString)) {
  178. return false;
  179. }
  180. if (is_integer($dateString) || is_numeric($dateString)) {
  181. $date = intval($dateString);
  182. } else {
  183. $date = strtotime($dateString);
  184. }
  185. if ($userOffset !== null) {
  186. return $this->convert($date, $userOffset);
  187. }
  188. if ($date === -1) {
  189. return false;
  190. }
  191. return $date;
  192. }
  193. /**
  194. * Returns a nicely formatted date string for given Datetime string.
  195. *
  196. * @param string $dateString Datetime string or Unix timestamp
  197. * @param int $userOffset User's offset from GMT (in hours)
  198. * @return string Formatted date string
  199. * @access public
  200. * @link http://book.cakephp.org/view/1471/Formatting
  201. */
  202. function nice($dateString = null, $userOffset = null) {
  203. if ($dateString != null) {
  204. $date = $this->fromString($dateString, $userOffset);
  205. } else {
  206. $date = time();
  207. }
  208. $format = $this->convertSpecifiers('%a, %b %eS %Y, %H:%M', $date);
  209. return strftime($format, $date);
  210. }
  211. /**
  212. * Returns a formatted descriptive date string for given datetime string.
  213. *
  214. * If the given date is today, the returned string could be "Today, 16:54".
  215. * If the given date was yesterday, the returned string could be "Yesterday, 16:54".
  216. * If $dateString's year is the current year, the returned string does not
  217. * include mention of the year.
  218. *
  219. * @param string $dateString Datetime string or Unix timestamp
  220. * @param int $userOffset User's offset from GMT (in hours)
  221. * @return string Described, relative date string
  222. * @access public
  223. * @link http://book.cakephp.org/view/1471/Formatting
  224. */
  225. function niceShort($dateString = null, $userOffset = null) {
  226. $date = $dateString ? $this->fromString($dateString, $userOffset) : time();
  227. $y = $this->isThisYear($date) ? '' : ' %Y';
  228. if ($this->isToday($dateString, $userOffset)) {
  229. $ret = sprintf(__('Today, %s',true), strftime("%H:%M", $date));
  230. } elseif ($this->wasYesterday($dateString, $userOffset)) {
  231. $ret = sprintf(__('Yesterday, %s',true), strftime("%H:%M", $date));
  232. } else {
  233. $format = $this->convertSpecifiers("%b %eS{$y}, %H:%M", $date);
  234. $ret = strftime($format, $date);
  235. }
  236. return $ret;
  237. }
  238. /**
  239. * Returns a partial SQL string to search for all records between two dates.
  240. *
  241. * @param string $dateString Datetime string or Unix timestamp
  242. * @param string $end Datetime string or Unix timestamp
  243. * @param string $fieldName Name of database field to compare with
  244. * @param int $userOffset User's offset from GMT (in hours)
  245. * @return string Partial SQL string.
  246. * @access public
  247. * @link http://book.cakephp.org/view/1471/Formatting
  248. */
  249. function daysAsSql($begin, $end, $fieldName, $userOffset = null) {
  250. $begin = $this->fromString($begin, $userOffset);
  251. $end = $this->fromString($end, $userOffset);
  252. $begin = date('Y-m-d', $begin) . ' 00:00:00';
  253. $end = date('Y-m-d', $end) . ' 23:59:59';
  254. return "($fieldName >= '$begin') AND ($fieldName <= '$end')";
  255. }
  256. /**
  257. * Returns a partial SQL string to search for all records between two times
  258. * occurring on the same day.
  259. *
  260. * @param string $dateString Datetime string or Unix timestamp
  261. * @param string $fieldName Name of database field to compare with
  262. * @param int $userOffset User's offset from GMT (in hours)
  263. * @return string Partial SQL string.
  264. * @access public
  265. * @link http://book.cakephp.org/view/1471/Formatting
  266. */
  267. function dayAsSql($dateString, $fieldName, $userOffset = null) {
  268. $date = $this->fromString($dateString, $userOffset);
  269. return $this->daysAsSql($dateString, $dateString, $fieldName);
  270. }
  271. /**
  272. * Returns true if given datetime string is today.
  273. *
  274. * @param string $dateString Datetime string or Unix timestamp
  275. * @param int $userOffset User's offset from GMT (in hours)
  276. * @return boolean True if datetime string is today
  277. * @access public
  278. */
  279. function isToday($dateString, $userOffset = null) {
  280. $date = $this->fromString($dateString, $userOffset);
  281. return date('Y-m-d', $date) == date('Y-m-d', time());
  282. }
  283. /**
  284. * Returns true if given datetime string is within this week
  285. * @param string $dateString
  286. * @param int $userOffset User's offset from GMT (in hours)
  287. * @return boolean True if datetime string is within current week
  288. * @access public
  289. * @link http://book.cakephp.org/view/1472/Testing-Time
  290. */
  291. function isThisWeek($dateString, $userOffset = null) {
  292. $date = $this->fromString($dateString, $userOffset);
  293. return date('W Y', $date) == date('W Y', time());
  294. }
  295. /**
  296. * Returns true if given datetime string is within this month
  297. * @param string $dateString
  298. * @param int $userOffset User's offset from GMT (in hours)
  299. * @return boolean True if datetime string is within current month
  300. * @access public
  301. * @link http://book.cakephp.org/view/1472/Testing-Time
  302. */
  303. function isThisMonth($dateString, $userOffset = null) {
  304. $date = $this->fromString($dateString);
  305. return date('m Y',$date) == date('m Y', time());
  306. }
  307. /**
  308. * Returns true if given datetime string is within current year.
  309. *
  310. * @param string $dateString Datetime string or Unix timestamp
  311. * @return boolean True if datetime string is within current year
  312. * @access public
  313. * @link http://book.cakephp.org/view/1472/Testing-Time
  314. */
  315. function isThisYear($dateString, $userOffset = null) {
  316. $date = $this->fromString($dateString, $userOffset);
  317. return date('Y', $date) == date('Y', time());
  318. }
  319. /**
  320. * Returns true if given datetime string was yesterday.
  321. *
  322. * @param string $dateString Datetime string or Unix timestamp
  323. * @param int $userOffset User's offset from GMT (in hours)
  324. * @return boolean True if datetime string was yesterday
  325. * @access public
  326. * @link http://book.cakephp.org/view/1472/Testing-Time
  327. *
  328. */
  329. function wasYesterday($dateString, $userOffset = null) {
  330. $date = $this->fromString($dateString, $userOffset);
  331. return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
  332. }
  333. /**
  334. * Returns true if given datetime string is tomorrow.
  335. *
  336. * @param string $dateString Datetime string or Unix timestamp
  337. * @param int $userOffset User's offset from GMT (in hours)
  338. * @return boolean True if datetime string was yesterday
  339. * @access public
  340. * @link http://book.cakephp.org/view/1472/Testing-Time
  341. */
  342. function isTomorrow($dateString, $userOffset = null) {
  343. $date = $this->fromString($dateString, $userOffset);
  344. return date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow'));
  345. }
  346. /**
  347. * Returns the quarter
  348. *
  349. * @param string $dateString
  350. * @param boolean $range if true returns a range in Y-m-d format
  351. * @return boolean True if datetime string is within current week
  352. * @access public
  353. * @link http://book.cakephp.org/view/1471/Formatting
  354. */
  355. function toQuarter($dateString, $range = false) {
  356. $time = $this->fromString($dateString);
  357. $date = ceil(date('m', $time) / 3);
  358. if ($range === true) {
  359. $range = 'Y-m-d';
  360. }
  361. if ($range !== false) {
  362. $year = date('Y', $time);
  363. switch ($date) {
  364. case 1:
  365. $date = array($year.'-01-01', $year.'-03-31');
  366. break;
  367. case 2:
  368. $date = array($year.'-04-01', $year.'-06-30');
  369. break;
  370. case 3:
  371. $date = array($year.'-07-01', $year.'-09-30');
  372. break;
  373. case 4:
  374. $date = array($year.'-10-01', $year.'-12-31');
  375. break;
  376. }
  377. }
  378. return $date;
  379. }
  380. /**
  381. * Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
  382. *
  383. * @param string $dateString Datetime string to be represented as a Unix timestamp
  384. * @param int $userOffset User's offset from GMT (in hours)
  385. * @return integer Unix timestamp
  386. * @access public
  387. * @link http://book.cakephp.org/view/1471/Formatting
  388. */
  389. function toUnix($dateString, $userOffset = null) {
  390. return $this->fromString($dateString, $userOffset);
  391. }
  392. /**
  393. * Returns a date formatted for Atom RSS feeds.
  394. *
  395. * @param string $dateString Datetime string or Unix timestamp
  396. * @param int $userOffset User's offset from GMT (in hours)
  397. * @return string Formatted date string
  398. * @access public
  399. * @link http://book.cakephp.org/view/1471/Formatting
  400. */
  401. function toAtom($dateString, $userOffset = null) {
  402. $date = $this->fromString($dateString, $userOffset);
  403. return date('Y-m-d\TH:i:s\Z', $date);
  404. }
  405. /**
  406. * Formats date for RSS feeds
  407. *
  408. * @param string $dateString Datetime string or Unix timestamp
  409. * @param int $userOffset User's offset from GMT (in hours)
  410. * @return string Formatted date string
  411. * @access public
  412. * @link http://book.cakephp.org/view/1471/Formatting
  413. */
  414. function toRSS($dateString, $userOffset = null) {
  415. $date = $this->fromString($dateString, $userOffset);
  416. if(!is_null($userOffset)) {
  417. if($userOffset == 0) {
  418. $timezone = '+0000';
  419. } else {
  420. $hours = (int) floor(abs($userOffset));
  421. $minutes = (int) (fmod(abs($userOffset), $hours) * 60);
  422. $timezone = ($userOffset < 0 ? '-' : '+') . str_pad($hours, 2, '0', STR_PAD_LEFT) . str_pad($minutes, 2, '0', STR_PAD_LEFT);
  423. }
  424. return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
  425. }
  426. return date("r", $date);
  427. }
  428. /**
  429. * Returns either a relative date or a formatted date depending
  430. * on the difference between the current time and given datetime.
  431. * $datetime should be in a <i>strtotime</i> - parsable format, like MySQL's datetime datatype.
  432. *
  433. * ### Options:
  434. *
  435. * - `format` => a fall back format if the relative time is longer than the duration specified by end
  436. * - `end` => The end of relative time telling
  437. * - `userOffset` => Users offset from GMT (in hours)
  438. *
  439. * Relative dates look something like this:
  440. * 3 weeks, 4 days ago
  441. * 15 seconds ago
  442. *
  443. * Default date formatting is d/m/yy e.g: on 18/2/09
  444. *
  445. * The returned string includes 'ago' or 'on' and assumes you'll properly add a word
  446. * like 'Posted ' before the function output.
  447. *
  448. * @param string $dateString Datetime string or Unix timestamp
  449. * @param array $options Default format if timestamp is used in $dateString
  450. * @return string Relative time string.
  451. * @access public
  452. * @link http://book.cakephp.org/view/1471/Formatting
  453. */
  454. function timeAgoInWords($dateTime, $options = array()) {
  455. $userOffset = null;
  456. if (is_array($options) && isset($options['userOffset'])) {
  457. $userOffset = $options['userOffset'];
  458. }
  459. $now = time();
  460. if (!is_null($userOffset)) {
  461. $now = $this->convert(time(), $userOffset);
  462. }
  463. $inSeconds = $this->fromString($dateTime, $userOffset);
  464. $backwards = ($inSeconds > $now);
  465. $format = 'j/n/y';
  466. $end = '+1 month';
  467. if (is_array($options)) {
  468. if (isset($options['format'])) {
  469. $format = $options['format'];
  470. unset($options['format']);
  471. }
  472. if (isset($options['end'])) {
  473. $end = $options['end'];
  474. unset($options['end']);
  475. }
  476. } else {
  477. $format = $options;
  478. }
  479. if ($backwards) {
  480. $futureTime = $inSeconds;
  481. $pastTime = $now;
  482. } else {
  483. $futureTime = $now;
  484. $pastTime = $inSeconds;
  485. }
  486. $diff = $futureTime - $pastTime;
  487. // If more than a week, then take into account the length of months
  488. if ($diff >= 604800) {
  489. $current = array();
  490. $date = array();
  491. list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime));
  492. list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $pastTime));
  493. $years = $months = $weeks = $days = $hours = $minutes = $seconds = 0;
  494. if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) {
  495. $months = 0;
  496. $years = 0;
  497. } else {
  498. if ($future['Y'] == $past['Y']) {
  499. $months = $future['m'] - $past['m'];
  500. } else {
  501. $years = $future['Y'] - $past['Y'];
  502. $months = $future['m'] + ((12 * $years) - $past['m']);
  503. if ($months >= 12) {
  504. $years = floor($months / 12);
  505. $months = $months - ($years * 12);
  506. }
  507. if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) {
  508. $years --;
  509. }
  510. }
  511. }
  512. if ($future['d'] >= $past['d']) {
  513. $days = $future['d'] - $past['d'];
  514. } else {
  515. $daysInPastMonth = date('t', $pastTime);
  516. $daysInFutureMonth = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y']));
  517. if (!$backwards) {
  518. $days = ($daysInPastMonth - $past['d']) + $future['d'];
  519. } else {
  520. $days = ($daysInFutureMonth - $past['d']) + $future['d'];
  521. }
  522. if ($future['m'] != $past['m']) {
  523. $months --;
  524. }
  525. }
  526. if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)) {
  527. $months = 11;
  528. $years --;
  529. }
  530. if ($months >= 12) {
  531. $years = $years + 1;
  532. $months = $months - 12;
  533. }
  534. if ($days >= 7) {
  535. $weeks = floor($days / 7);
  536. $days = $days - ($weeks * 7);
  537. }
  538. } else {
  539. $years = $months = $weeks = 0;
  540. $days = floor($diff / 86400);
  541. $diff = $diff - ($days * 86400);
  542. $hours = floor($diff / 3600);
  543. $diff = $diff - ($hours * 3600);
  544. $minutes = floor($diff / 60);
  545. $diff = $diff - ($minutes * 60);
  546. $seconds = $diff;
  547. }
  548. $relativeDate = '';
  549. $diff = $futureTime - $pastTime;
  550. if ($diff > abs($now - $this->fromString($end))) {
  551. $relativeDate = sprintf(__('on %s',true), date($format, $inSeconds));
  552. } else {
  553. if ($years > 0) {
  554. // years and months and days
  555. $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d year', '%d years', $years, true), $years);
  556. $relativeDate .= $months > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d month', '%d months', $months, true), $months) : '';
  557. $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks) : '';
  558. $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : '';
  559. } elseif (abs($months) > 0) {
  560. // months, weeks and days
  561. $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d month', '%d months', $months, true), $months);
  562. $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks) : '';
  563. $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : '';
  564. } elseif (abs($weeks) > 0) {
  565. // weeks and days
  566. $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks);
  567. $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : '';
  568. } elseif (abs($days) > 0) {
  569. // days and hours
  570. $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days);
  571. $relativeDate .= $hours > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d hour', '%d hours', $hours, true), $hours) : '';
  572. } elseif (abs($hours) > 0) {
  573. // hours and minutes
  574. $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d hour', '%d hours', $hours, true), $hours);
  575. $relativeDate .= $minutes > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d minute', '%d minutes', $minutes, true), $minutes) : '';
  576. } elseif (abs($minutes) > 0) {
  577. // minutes only
  578. $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d minute', '%d minutes', $minutes, true), $minutes);
  579. } else {
  580. // seconds only
  581. $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d second', '%d seconds', $seconds, true), $seconds);
  582. }
  583. if (!$backwards) {
  584. $relativeDate = sprintf(__('%s ago', true), $relativeDate);
  585. }
  586. }
  587. return $relativeDate;
  588. }
  589. /**
  590. * Alias for timeAgoInWords
  591. *
  592. * @param mixed $dateTime Datetime string (strtotime-compatible) or Unix timestamp
  593. * @param mixed $options Default format string, if timestamp is used in $dateTime, or an array of options to be passed
  594. * on to timeAgoInWords().
  595. * @return string Relative time string.
  596. * @see TimeHelper::timeAgoInWords
  597. * @access public
  598. * @deprecated This method alias will be removed in future versions.
  599. * @link http://book.cakephp.org/view/1471/Formatting
  600. */
  601. function relativeTime($dateTime, $options = array()) {
  602. return $this->timeAgoInWords($dateTime, $options);
  603. }
  604. /**
  605. * Returns true if specified datetime was within the interval specified, else false.
  606. *
  607. * @param mixed $timeInterval the numeric value with space then time type.
  608. * Example of valid types: 6 hours, 2 days, 1 minute.
  609. * @param mixed $dateString the datestring or unix timestamp to compare
  610. * @param int $userOffset User's offset from GMT (in hours)
  611. * @return bool
  612. * @access public
  613. * @link http://book.cakephp.org/view/1472/Testing-Time
  614. */
  615. function wasWithinLast($timeInterval, $dateString, $userOffset = null) {
  616. $tmp = str_replace(' ', '', $timeInterval);
  617. if (is_numeric($tmp)) {
  618. $timeInterval = $tmp . ' ' . __('days', true);
  619. }
  620. $date = $this->fromString($dateString, $userOffset);
  621. $interval = $this->fromString('-'.$timeInterval);
  622. if ($date >= $interval && $date <= time()) {
  623. return true;
  624. }
  625. return false;
  626. }
  627. /**
  628. * Returns gmt, given either a UNIX timestamp or a valid strtotime() date string.
  629. *
  630. * @param string $dateString Datetime string
  631. * @return string Formatted date string
  632. * @access public
  633. * @link http://book.cakephp.org/view/1471/Formatting
  634. */
  635. function gmt($string = null) {
  636. if ($string != null) {
  637. $string = $this->fromString($string);
  638. } else {
  639. $string = time();
  640. }
  641. $string = $this->fromString($string);
  642. $hour = intval(date("G", $string));
  643. $minute = intval(date("i", $string));
  644. $second = intval(date("s", $string));
  645. $month = intval(date("n", $string));
  646. $day = intval(date("j", $string));
  647. $year = intval(date("Y", $string));
  648. return gmmktime($hour, $minute, $second, $month, $day, $year);
  649. }
  650. /**
  651. * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  652. * This function also accepts a time string and a format string as first and second parameters.
  653. * In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
  654. *
  655. * @param string $format date format string (or a DateTime string)
  656. * @param string $dateString Datetime string (or a date format string)
  657. * @param boolean $invalid flag to ignore results of fromString == false
  658. * @param int $userOffset User's offset from GMT (in hours)
  659. * @return string Formatted date string
  660. * @access public
  661. */
  662. function format($format, $date = null, $invalid = false, $userOffset = null) {
  663. $time = $this->fromString($date, $userOffset);
  664. $_time = $this->fromString($format, $userOffset);
  665. if (is_numeric($_time) && $time === false) {
  666. $format = $date;
  667. return $this->i18nFormat($_time, $format, $invalid, $userOffset);
  668. }
  669. if ($time === false && $invalid !== false) {
  670. return $invalid;
  671. }
  672. return date($format, $time);
  673. }
  674. /**
  675. * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  676. * It take in account the default date format for the current language if a LC_TIME file is used.
  677. *
  678. * @param string $dateString Datetime string
  679. * @param string $format strftime format string.
  680. * @param boolean $invalid flag to ignore results of fromString == false
  681. * @param int $userOffset User's offset from GMT (in hours)
  682. * @return string Formatted and translated date string @access public
  683. * @access public
  684. */
  685. function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) {
  686. $date = $this->fromString($date, $userOffset);
  687. if ($date === false && $invalid !== false) {
  688. return $invalid;
  689. }
  690. if (empty($format)) {
  691. $format = '%x';
  692. }
  693. $format = $this->convertSpecifiers($format, $date);
  694. return strftime($format, $date);
  695. }
  696. }