Inheritance: extends DateTimeInterfac\DateTimeInterface
 /**
  * Get the difference in a human readable format.
  *
  * @param \Cake\Chronos\ChronosInterface $date The datetime to start with.
  * @param \Cake\Chronos\ChronosInterface|null $other The datetime to compare against.
  * @param bool $absolute removes time difference modifiers ago, after, etc
  * @return string The difference between the two days in a human readable format
  * @see \Cake\Chronos\ChronosInterface::diffForHumans
  */
 public function diffForHumans(ChronosInterface $date, ChronosInterface $other = null, $absolute = false)
 {
     $isNow = $other === null;
     if ($isNow) {
         $other = $date->now($date->tz);
     }
     $diffInterval = $date->diff($other);
     switch (true) {
         case $diffInterval->y > 0:
             $count = $diffInterval->y;
             $message = __dn('cake', '{0} year', '{0} years', $count, $count);
             break;
         case $diffInterval->m > 0:
             $count = $diffInterval->m;
             $message = __dn('cake', '{0} month', '{0} months', $count, $count);
             break;
         case $diffInterval->d > 0:
             $count = $diffInterval->d;
             if ($count >= ChronosInterface::DAYS_PER_WEEK) {
                 $count = (int) ($count / ChronosInterface::DAYS_PER_WEEK);
                 $message = __dn('cake', '{0} week', '{0} weeks', $count, $count);
             } else {
                 $message = __dn('cake', '{0} day', '{0} days', $count, $count);
             }
             break;
         case $diffInterval->h > 0:
             $count = $diffInterval->h;
             $message = __dn('cake', '{0} hour', '{0} hours', $count, $count);
             break;
         case $diffInterval->i > 0:
             $count = $diffInterval->i;
             $message = __dn('cake', '{0} minute', '{0} minutes', $count, $count);
             break;
         default:
             $count = $diffInterval->s;
             $message = __dn('cake', '{0} second', '{0} seconds', $count, $count);
             break;
     }
     if ($absolute) {
         return $message;
     }
     $isFuture = $diffInterval->invert === 1;
     if ($isNow) {
         return $isFuture ? __d('cake', '{0} from now', $message) : __d('cake', '{0} ago', $message);
     }
     return $isFuture ? __d('cake', '{0} after', $message) : __d('cake', '{0} before', $message);
 }
示例#2
0
 /**
  * Checks if the passed in date is the same day as the instance current day.
  *
  * @param ChronosInterface $dt The instance to check against.
  * @return bool
  */
 public function isSameDay(ChronosInterface $dt)
 {
     return $this->toDateString() === $dt->toDateString();
 }