PageRenderTime 65ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

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