TimeHelper.php 23 KB

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