Example #1
0
 /**
  * Test that difference() returns the difference of seconds between 2 dates.
  */
 public function testDifference()
 {
     $this->assertEquals(-60, Time::difference(null, '+60 seconds'));
     $this->assertEquals(-3600, Time::difference(new DateTime(), '+1 hour'));
     $this->assertEquals(14400, Time::difference('+5 hours', '+1 hour'));
 }
Example #2
0
 /**
  * Format a timestamp as a date relative human readable string; also known as time ago in words.
  *
  * @uses Titon\Utility\Time
  *
  * @param string|int $time
  * @param array $options
  * @return string
  */
 public static function relativeTime($time, array $options = array())
 {
     $defaults = array('seconds' => array('%ss', '%s second', '%s seconds'), 'minutes' => array('%sm', '%s minute', '%s minutes'), 'hours' => array('%sh', '%s hour', '%s hours'), 'days' => array('%sd', '%s day', '%s days'), 'weeks' => array('%sw', '%s week', '%s weeks'), 'months' => array('%sm', '%s month', '%s months'), 'years' => array('%sy', '%s year', '%s years'), 'now' => 'just now', 'in' => 'in %s', 'ago' => '%s ago', 'separator' => ', ', 'verbose' => true, 'depth' => 2, 'time' => time());
     $options = $options + $defaults;
     $diff = Time::difference($options['time'], Time::toUnix($time));
     $output = array();
     // Present tense
     if ($diff === 0) {
         return $options['now'];
     }
     // Past or future tense
     $seconds = abs($diff);
     $depth = $options['depth'];
     while ($seconds > 0 && $depth > 0) {
         if ($seconds >= Time::YEAR) {
             $key = 'years';
             $div = Time::YEAR;
         } else {
             if ($seconds >= Time::MONTH) {
                 $key = 'months';
                 $div = Time::MONTH;
             } else {
                 if ($seconds >= Time::WEEK) {
                     $key = 'weeks';
                     $div = Time::WEEK;
                 } else {
                     if ($seconds >= Time::DAY) {
                         $key = 'days';
                         $div = Time::DAY;
                     } else {
                         if ($seconds >= Time::HOUR) {
                             $key = 'hours';
                             $div = Time::HOUR;
                         } else {
                             if ($seconds >= Time::MINUTE) {
                                 $key = 'minutes';
                                 $div = Time::MINUTE;
                             } else {
                                 $key = 'seconds';
                                 $div = Time::SECOND;
                             }
                         }
                     }
                 }
             }
         }
         $count = round($seconds / $div);
         $seconds -= $count * $div;
         if ($options['verbose']) {
             $index = $count == 1 ? 1 : 2;
         } else {
             $index = 0;
         }
         $output[$key] = sprintf($options[$key][$index], $count);
         $depth--;
     }
     $output = implode($options['separator'], $output);
     // Past
     if ($diff > 0) {
         return sprintf($options['ago'], $output);
     }
     return sprintf($options['in'], $output);
 }