public function timeAgoInWords($dateTime, $options = array()) { if (!isset($this->Asset) || !$this->settings['relativeTime']['enabled'] || isset($options['useCore'])) { unset($options['useCore']); return parent::timeAgoInWords($dateTime, $options); } if (!$this->_jsAdded) { if ($this->settings['relativeTime']['jsPacket'] !== false) { $this->Asset->js(array('jquery.timeago', 'jquery.mi.relativeTime'), $this->settings['relativeTime']['jsPacket']); } $this->_jsAdded = true; } $date = date('Y-m-d H:i:s O', $this->fromString($dateTime)); return String::insert($this->settings['relativeTime']['tag'], array_merge($options, compact('date'))); }
/** * Test element wrapping in timeAgoInWords * * @return void */ public function testTimeAgoInWords() { $Time = new TimeHelper($this->View); $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days'); $result = $Time->timeAgoInWords($timestamp, array('end' => '1 years', 'element' => 'span')); $expected = array('span' => array('title' => $timestamp, 'class' => 'time-ago-in-words'), 'on ' . date('j/n/y', $timestamp), '/span'); $this->assertTags($result, $expected); $result = $Time->timeAgoInWords($timestamp, array('end' => '1 years', 'element' => array('title' => 'testing', 'rel' => 'test'))); $expected = array('span' => array('title' => 'testing', 'class' => 'time-ago-in-words', 'rel' => 'test'), 'on ' . date('j/n/y', $timestamp), '/span'); $this->assertTags($result, $expected); $timestamp = strtotime('+2 weeks'); $result = $Time->timeAgoInWords($timestamp, array('end' => '1 years', 'element' => 'div')); $expected = array('div' => array('title' => $timestamp, 'class' => 'time-ago-in-words'), '2 weeks', '/div'); $this->assertTags($result, $expected); }
/** * Wrapper to Time->timeAgoInWords() * @param $var * @return mixed */ function cakeAgo($var) { $time = new TimeHelper(); return $time->timeAgoInWords($var); }
/** * Returns either a relative date or a formatted date depending * on the difference between the current time and given datetime. * $datetime should be in a <i>strtotime</i> - parsable format, like MySQL's datetime datatype. * * ### Options: * * - `format` => a fall back format if the relative time is longer than the duration specified by end * - `end` => The end of relative time telling * - `userOffset` => Users offset from GMT (in hours) * * Relative dates look something like this: * 3 weeks, 4 days ago * 15 seconds ago * * Default date formatting is d/m/yy e.g: on 18/2/09 * * The returned string includes 'ago' or 'on' and assumes you'll properly add a word * like 'Posted ' before the function output. * * @param string $dateString Datetime string or Unix timestamp * @param array $options Default format if timestamp is used in $dateString * @return string Relative time string. * @access public * @link http://book.cakephp.org/view/1471/Formatting */ function timeAgoInWords($dateTime, $options = array()) { return parent::timeAgoInWords($dateTime, $this->__userOffset($dateTime, $options)); }