TimeHelper.php 24 KB

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