PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/cgajardo/repositorium
PHP | 744 lines | 398 code | 58 blank | 288 comment | 91 complexity | 4d4965838a731007f1925d12991a9a8e 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 '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. return date("r", $date);
  417. }
  418. /**
  419. * Returns either a relative date or a formatted date depending
  420. * on the difference between the current time and given datetime.
  421. * $datetime should be in a <i>strtotime</i> - parsable format, like MySQL's datetime datatype.
  422. *
  423. * ### Options:
  424. *
  425. * - `format` => a fall back format if the relative time is longer than the duration specified by end
  426. * - `end` => The end of relative time telling
  427. * - `userOffset` => Users offset from GMT (in hours)
  428. *
  429. * Relative dates look something like this:
  430. * 3 weeks, 4 days ago
  431. * 15 seconds ago
  432. *
  433. * Default date formatting is d/m/yy e.g: on 18/2/09
  434. *
  435. * The returned string includes 'ago' or 'on' and assumes you'll properly add a word
  436. * like 'Posted ' before the function output.
  437. *
  438. * @param string $dateString Datetime string or Unix timestamp
  439. * @param array $options Default format if timestamp is used in $dateString
  440. * @return string Relative time string.
  441. * @access public
  442. * @link http://book.cakephp.org/view/1471/Formatting
  443. */
  444. function timeAgoInWords($dateTime, $options = array()) {
  445. $userOffset = null;
  446. if (is_array($options) && isset($options['userOffset'])) {
  447. $userOffset = $options['userOffset'];
  448. }
  449. $now = time();
  450. if (!is_null($userOffset)) {
  451. $now = $this->convert(time(), $userOffset);
  452. }
  453. $inSeconds = $this->fromString($dateTime, $userOffset);
  454. $backwards = ($inSeconds > $now);
  455. $format = 'j/n/y';
  456. $end = '+1 month';
  457. if (is_array($options)) {
  458. if (isset($options['format'])) {
  459. $format = $options['format'];
  460. unset($options['format']);
  461. }
  462. if (isset($options['end'])) {
  463. $end = $options['end'];
  464. unset($options['end']);
  465. }
  466. } else {
  467. $format = $options;
  468. }
  469. if ($backwards) {
  470. $futureTime = $inSeconds;
  471. $pastTime = $now;
  472. } else {
  473. $futureTime = $now;
  474. $pastTime = $inSeconds;
  475. }
  476. $diff = $futureTime - $pastTime;
  477. // If more than a week, then take into account the length of months
  478. if ($diff >= 604800) {
  479. $current = array();
  480. $date = array();
  481. list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime));
  482. list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $pastTime));
  483. $years = $months = $weeks = $days = $hours = $minutes = $seconds = 0;
  484. if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) {
  485. $months = 0;
  486. $years = 0;
  487. } else {
  488. if ($future['Y'] == $past['Y']) {
  489. $months = $future['m'] - $past['m'];
  490. } else {
  491. $years = $future['Y'] - $past['Y'];
  492. $months = $future['m'] + ((12 * $years) - $past['m']);
  493. if ($months >= 12) {
  494. $years = floor($months / 12);
  495. $months = $months - ($years * 12);
  496. }
  497. if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) {
  498. $years --;
  499. }
  500. }
  501. }
  502. if ($future['d'] >= $past['d']) {
  503. $days = $future['d'] - $past['d'];
  504. } else {
  505. $daysInPastMonth = date('t', $pastTime);
  506. $daysInFutureMonth = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y']));
  507. if (!$backwards) {
  508. $days = ($daysInPastMonth - $past['d']) + $future['d'];
  509. } else {
  510. $days = ($daysInFutureMonth - $past['d']) + $future['d'];
  511. }
  512. if ($future['m'] != $past['m']) {
  513. $months --;
  514. }
  515. }
  516. if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)) {
  517. $months = 11;
  518. $years --;
  519. }
  520. if ($months >= 12) {
  521. $years = $years + 1;
  522. $months = $months - 12;
  523. }
  524. if ($days >= 7) {
  525. $weeks = floor($days / 7);
  526. $days = $days - ($weeks * 7);
  527. }
  528. } else {
  529. $years = $months = $weeks = 0;
  530. $days = floor($diff / 86400);
  531. $diff = $diff - ($days * 86400);
  532. $hours = floor($diff / 3600);
  533. $diff = $diff - ($hours * 3600);
  534. $minutes = floor($diff / 60);
  535. $diff = $diff - ($minutes * 60);
  536. $seconds = $diff;
  537. }
  538. $relativeDate = '';
  539. $diff = $futureTime - $pastTime;
  540. if ($diff > abs($now - $this->fromString($end))) {
  541. $relativeDate = sprintf(__('on %s',true), date($format, $inSeconds));
  542. } else {
  543. if ($years > 0) {
  544. // years and months and days
  545. $relativeDate .= ($relativeDate ? ', ' : '') . $years . ' ' . __n('year', 'years', $years, true);
  546. $relativeDate .= $months > 0 ? ($relativeDate ? ', ' : '') . $months . ' ' . __n('month', 'months', $months, true) : '';
  547. $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . $weeks . ' ' . __n('week', 'weeks', $weeks, true) : '';
  548. $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . __n('day', 'days', $days, true) : '';
  549. } elseif (abs($months) > 0) {
  550. // months, weeks and days
  551. $relativeDate .= ($relativeDate ? ', ' : '') . $months . ' ' . __n('month', 'months', $months, true);
  552. $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . $weeks . ' ' . __n('week', 'weeks', $weeks, true) : '';
  553. $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . __n('day', 'days', $days, true) : '';
  554. } elseif (abs($weeks) > 0) {
  555. // weeks and days
  556. $relativeDate .= ($relativeDate ? ', ' : '') . $weeks . ' ' . __n('week', 'weeks', $weeks, true);
  557. $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . __n('day', 'days', $days, true) : '';
  558. } elseif (abs($days) > 0) {
  559. // days and hours
  560. $relativeDate .= ($relativeDate ? ', ' : '') . $days . ' ' . __n('day', 'days', $days, true);
  561. $relativeDate .= $hours > 0 ? ($relativeDate ? ', ' : '') . $hours . ' ' . __n('hour', 'hours', $hours, true) : '';
  562. } elseif (abs($hours) > 0) {
  563. // hours and minutes
  564. $relativeDate .= ($relativeDate ? ', ' : '') . $hours . ' ' . __n('hour', 'hours', $hours, true);
  565. $relativeDate .= $minutes > 0 ? ($relativeDate ? ', ' : '') . $minutes . ' ' . __n('minute', 'minutes', $minutes, true) : '';
  566. } elseif (abs($minutes) > 0) {
  567. // minutes only
  568. $relativeDate .= ($relativeDate ? ', ' : '') . $minutes . ' ' . __n('minute', 'minutes', $minutes, true);
  569. } else {
  570. // seconds only
  571. $relativeDate .= ($relativeDate ? ', ' : '') . $seconds . ' ' . __n('second', 'seconds', $seconds, true);
  572. }
  573. if (!$backwards) {
  574. $relativeDate = sprintf(__('%s ago', true), $relativeDate);
  575. }
  576. }
  577. return $relativeDate;
  578. }
  579. /**
  580. * Alias for timeAgoInWords
  581. *
  582. * @param mixed $dateTime Datetime string (strtotime-compatible) or Unix timestamp
  583. * @param mixed $options Default format string, if timestamp is used in $dateTime, or an array of options to be passed
  584. * on to timeAgoInWords().
  585. * @return string Relative time string.
  586. * @see TimeHelper::timeAgoInWords
  587. * @access public
  588. * @deprecated This method alias will be removed in future versions.
  589. * @link http://book.cakephp.org/view/1471/Formatting
  590. */
  591. function relativeTime($dateTime, $options = array()) {
  592. return $this->timeAgoInWords($dateTime, $options);
  593. }
  594. /**
  595. * Returns true if specified datetime was within the interval specified, else false.
  596. *
  597. * @param mixed $timeInterval the numeric value with space then time type.
  598. * Example of valid types: 6 hours, 2 days, 1 minute.
  599. * @param mixed $dateString the datestring or unix timestamp to compare
  600. * @param int $userOffset User's offset from GMT (in hours)
  601. * @return bool
  602. * @access public
  603. * @link http://book.cakephp.org/view/1472/Testing-Time
  604. */
  605. function wasWithinLast($timeInterval, $dateString, $userOffset = null) {
  606. $tmp = str_replace(' ', '', $timeInterval);
  607. if (is_numeric($tmp)) {
  608. $timeInterval = $tmp . ' ' . __('days', true);
  609. }
  610. $date = $this->fromString($dateString, $userOffset);
  611. $interval = $this->fromString('-'.$timeInterval);
  612. if ($date >= $interval && $date <= time()) {
  613. return true;
  614. }
  615. return false;
  616. }
  617. /**
  618. * Returns gmt, given either a UNIX timestamp or a valid strtotime() date string.
  619. *
  620. * @param string $dateString Datetime string
  621. * @return string Formatted date string
  622. * @access public
  623. * @link http://book.cakephp.org/view/1471/Formatting
  624. */
  625. function gmt($string = null) {
  626. if ($string != null) {
  627. $string = $this->fromString($string);
  628. } else {
  629. $string = time();
  630. }
  631. $string = $this->fromString($string);
  632. $hour = intval(date("G", $string));
  633. $minute = intval(date("i", $string));
  634. $second = intval(date("s", $string));
  635. $month = intval(date("n", $string));
  636. $day = intval(date("j", $string));
  637. $year = intval(date("Y", $string));
  638. return gmmktime($hour, $minute, $second, $month, $day, $year);
  639. }
  640. /**
  641. * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  642. * This function also accepts a time string and a format string as first and second parameters.
  643. * In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
  644. *
  645. * @param string $format date format string (or a DateTime string)
  646. * @param string $dateString Datetime string (or a date format string)
  647. * @param boolean $invalid flag to ignore results of fromString == false
  648. * @param int $userOffset User's offset from GMT (in hours)
  649. * @return string Formatted date string
  650. * @access public
  651. */
  652. function format($format, $date = null, $invalid = false, $userOffset = null) {
  653. $time = $this->fromString($date, $userOffset);
  654. $_time = $this->fromString($format, $userOffset);
  655. if (is_numeric($_time) && $time === false) {
  656. $format = $date;
  657. return $this->i18nFormat($_time, $format, $invalid, $userOffset);
  658. }
  659. if ($time === false && $invalid !== false) {
  660. return $invalid;
  661. }
  662. return date($format, $time);
  663. }
  664. /**
  665. * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  666. * It take in account the default date format for the current language if a LC_TIME file is used.
  667. *
  668. * @param string $dateString Datetime string
  669. * @param string $format strftime format string.
  670. * @param boolean $invalid flag to ignore results of fromString == false
  671. * @param int $userOffset User's offset from GMT (in hours)
  672. * @return string Formatted and translated date string @access public
  673. * @access public
  674. */
  675. function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) {
  676. $date = $this->fromString($date, $userOffset);
  677. if ($date === false && $invalid !== false) {
  678. return $invalid;
  679. }
  680. if (empty($format)) {
  681. $format = '%x';
  682. }
  683. $format = $this->convertSpecifiers($format, $date);
  684. return strftime($format, $date);
  685. }
  686. }