Exemple #1
0
 /**
  * @param int      $time
  * @param int|null $from
  * @return null|string
  */
 public static function timeAgo($time, $from = NULL)
 {
     $from = DateTime::from($from);
     $time = DateTime::from($time);
     if ($from === NULL) {
         $from = time();
     }
     $diff = $from->getTimestamp() - $time->getTimestamp();
     if ($diff < 0) {
         return NULL;
     }
     $callback = self::$timeAgoCallback;
     if ($diff < 60) {
         return $callback('now', 0);
     } else {
         if ($diff < 3601) {
             return $callback('minutes', floor($diff / 60));
         } else {
             if ($diff < 86401) {
                 return $callback('hours', floor($diff / 3600));
             } else {
                 if ($diff < 2629801) {
                     return $callback('days', floor($diff / 86400));
                 } else {
                     if ($diff < 31536001) {
                         return $callback('months', floor($diff / 2629800));
                     } else {
                         return $callback('years', floor($diff / 31536000));
                     }
                 }
             }
         }
     }
 }
Exemple #2
0
 public function testEnd()
 {
     $v = array(date('Y'), date('m'), date('d'), date('H'), date('i'), date('s'));
     $this->assertSame("{$v['0']}-12-31 23:59:59", Date::end('year')->baseFormat());
     $this->assertSame("{$v['0']}-{$v['1']}-31 23:59:59", Date::end('month')->baseFormat());
     $this->assertSame("{$v['0']}-{$v['1']}-{$v['2']} 23:59:59", Date::end('day')->baseFormat());
     $this->assertSame("{$v['0']}-{$v['1']}-{$v['2']} {$v['3']}:59:59", Date::end('hours')->baseFormat());
     $this->assertSame("{$v['0']}-{$v['1']}-{$v['2']} {$v['3']}:{$v['4']}:59", Date::end('minutes')->baseFormat());
     $this->assertSame("{$v['0']}-{$v['1']}-{$v['2']} {$v['3']}:{$v['4']}:{$v['5']}", Date::end('seconds')->baseFormat());
     $this->assertSame("{$v['0']}-{$v['1']}-19 23:59:59", Date::end('day', 19)->baseFormat());
 }