/** * Test output timezone with timeAgoInWords * * @return void */ public function testTimeAgoInWordsOutputTimezone() { $Time = new TimeHelper($this->View, ['outputTimezone' => 'America/Vancouver']); $timestamp = new Time('+8 years, +4 months +2 weeks +3 days'); $result = $Time->timeAgoInWords($timestamp, ['end' => '1 years', 'element' => 'span']); $vancouver = clone $timestamp; $vancouver->timezone('America/Vancouver'); $expected = ['span' => ['title' => $vancouver->__toString(), 'class' => 'time-ago-in-words'], 'on ' . $vancouver->format('n/j/y'), '/span']; $this->assertHtml($expected, $result); }
/** * 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('n/j/y', $timestamp), '/span'); $this->assertHtml($expected, $result); $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('n/j/y', $timestamp), '/span'); $this->assertHtml($expected, $result); $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->assertHtml($expected, $result); }
/** * Returns a nicely formatted date string for given Datetime string. * * @param int|string|\DateTime|null $dateString UNIX timestamp, strtotime() valid string or DateTime object * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object * @param string|null $locale Locale string. * @param string|bool $default Default string to use when no dateString is given. Use false to allow null as current date. * @return string Formatted date string */ public function nice($dateString = null, $timezone = null, $locale = null, $default = '') { if ($dateString === null && $default !== false) { return $default; } return parent::nice($dateString, $timezone, $locale); }
/** * Default Constructor * * ### Settings: * * - `engine` Class name to use to replace Cake\I18n\Time functionality * The class needs to be placed in the `Utility` directory. * * @param \Cake\View\View $View The View this helper is being attached to. * @param array $config Configuration settings for the helper * @throws \Cake\Core\Exception\Exception When the engine class could not be found. */ public function __construct(View $View, array $config = []) { parent::__construct($View, $config); $config = $this->_config; $engineClass = App::className($config['engine'], 'Utility'); if ($engineClass) { $this->_engine = new $engineClass($config); } else { throw new Exception(sprintf('Class for %s could not be found', $config['engine'])); } }