/** * {@inheritdoc} * * @uses Titon\Common\Registry */ public static function relativeTime($time, array $options = array()) { $g11n = G11n::registry(); $msg = function ($key) use($g11n) { return $g11n->translate('core.format.relativeTime.' . $key); }; // TODO Find a more optimized way to do this. $options = $options + array('seconds' => array($msg('sec'), $msg('second'), $msg('seconds')), 'minutes' => array($msg('min'), $msg('minute'), $msg('minutes')), 'hours' => array($msg('hr'), $msg('hour'), $msg('hours')), 'days' => array($msg('dy'), $msg('day'), $msg('days')), 'weeks' => array($msg('wk'), $msg('week'), $msg('weeks')), 'months' => array($msg('mon'), $msg('month'), $msg('months')), 'years' => array($msg('yr'), $msg('year'), $msg('years')), 'now' => $msg('now'), 'in' => $msg('in'), 'ago' => $msg('ago')); return parent::relativeTime($time, $options); }
/** * Test that relativeTime() returns a timestamp in seconds ago/in. */ public function testRelativeTime() { $time = mktime(16, 35, 0, 2, 26, 2012); // Current $this->assertEquals('just now', Format::relativeTime(strtotime('+0 seconds', $time), array('time' => $time))); $this->assertEquals('just now', Format::relativeTime($time, array('time' => $time))); $this->assertEquals('in 2 hours', Format::relativeTime(strtotime('+2 hours', $time), array('time' => $time))); $this->assertEquals('in 45 minutes', Format::relativeTime(strtotime('+45 minutes', $time), array('time' => $time))); // Past $this->assertEquals('45 seconds ago', Format::relativeTime(strtotime('-45 seconds', $time), array('time' => $time))); $this->assertEquals('2 weeks, 2 days ago', Format::relativeTime(strtotime('-16 days', $time), array('time' => $time))); $this->assertEquals('8 months ago', Format::relativeTime(strtotime('-33 weeks', $time), array('time' => $time))); $this->assertEquals('6 months, 4 days ago', Format::relativeTime(strtotime('-6 months', $time), array('time' => $time))); $this->assertEquals('2 years, 2 months ago', Format::relativeTime(strtotime('-799 days', $time), array('time' => $time))); // Future $this->assertEquals('in 45 seconds', Format::relativeTime(strtotime('+45 seconds', $time), array('time' => $time))); $this->assertEquals('in 2 weeks, 2 days', Format::relativeTime(strtotime('+16 days', $time), array('time' => $time))); $this->assertEquals('in 8 months', Format::relativeTime(strtotime('+33 weeks', $time), array('time' => $time))); $this->assertEquals('in 6 months, 2 days', Format::relativeTime(strtotime('+6 months', $time), array('time' => $time))); $this->assertEquals('in 2 years, 2 months', Format::relativeTime(strtotime('+799 days', $time), array('time' => $time))); // Large depth $this->assertEquals('1 year, 1 month, 4 days ago', Format::relativeTime(strtotime('-399 days', $time), array('time' => $time, 'depth' => 5))); $this->assertEquals('1 year, 3 months ago', Format::relativeTime(strtotime('-444 days', $time), array('time' => $time, 'depth' => 5))); $this->assertEquals('3 years ago', Format::relativeTime(strtotime('-999 days', $time), array('time' => $time, 'depth' => 5))); $this->assertEquals('3 years, 5 months ago', Format::relativeTime(strtotime('-1235 days', $time), array('time' => $time, 'depth' => 5))); $this->assertEquals('2 years, 2 months, 1 week, 2 days ago', Format::relativeTime(strtotime('-799 days', $time), array('time' => $time, 'depth' => 5))); // Non-verbose $this->assertEquals('2y, 2m, 1w, 2d ago', Format::relativeTime(strtotime('-799 days', $time), array('time' => $time, 'depth' => 5, 'verbose' => false))); $this->assertEquals('in 2y, 2m, 1w, 2d', Format::relativeTime(strtotime('+799 days', $time), array('time' => $time, 'depth' => 5, 'verbose' => false))); }