TimeHelper.php 25 KB

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