/**
  * Returns a formatted diff for the given from and to datetimes
  *
  * @param  DateTimeInterface $from
  * @param  DateTimeInterface $to
  *
  * @return string
  */
 public function formatDiff(DateTimeInterface $from, DateTimeInterface $to)
 {
     static $units = array('y' => 'year', 'm' => 'month', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second');
     $diff = $to->diff($from);
     foreach ($units as $attribute => $unit) {
         $count = $diff->{$attribute};
         if (0 !== $count) {
             return $this->doGetDiffMessage($count, $diff->invert, $unit);
         }
     }
     return $this->getEmptyDiffMessage();
 }
Esempio n. 2
0
 /**
  * Compare two Period objects according to their duration.
  *
  * @param \Thin\Period $period
  *
  * @return int
  */
 public function compareDuration(Period $period)
 {
     $datetime = new DateTime();
     $alt = clone $datetime;
     $datetime->add($this->start->diff($this->end));
     $alt->add($period->start->diff($period->end));
     if ($datetime > $alt) {
         return 1;
     } elseif ($datetime < $alt) {
         return -1;
     }
     return 0;
 }
Esempio n. 3
0
 private static function toRelative(\DateTimeInterface $dateTime, \DateTimeInterface $now = null)
 {
     $precision = 'minute';
     $now = $now ?: new \DateTimeImmutable();
     if ($dateTime == $now) {
         return 'now';
     }
     $diff = $dateTime->diff($now);
     if ($dateTime->format('Y-m-d') != $now->format('Y-m-d')) {
         $days = (new \DateTime($dateTime->format('Y-m-d')))->diff(new \DateTime($now->format('Y-m-d')))->days;
         if ($days == 1) {
             $dayString = 'tomorrow';
         } else {
             if ($days < 7) {
                 $dayString = $dateTime->format('l');
             } else {
                 $dayString = $dateTime->format('Y-m-d');
             }
         }
         return $dayString . ' at ' . $dateTime->format('H:i');
     }
     $times = [];
     foreach (['hour' => $diff->h, 'minute' => $diff->i, 'second' => $diff->s] as $unit => $value) {
         if ($value) {
             $times[] = $value . ' ' . $unit . ($value == 1 ? '' : 's');
         }
         if ($precision == $unit) {
             break;
         }
     }
     return 'in ' . implode(', ', $times);
 }
Esempio n. 4
0
function date_diff(DateTimeInterface $datetime, DateTimeInterface $datetime2, bool $absolute = false)
{
    return $datetime->diff($datetime2, $absolute);
}
Esempio n. 5
0
 public function timeRange(\DateTimeInterface $start, \DateTimeInterface $end)
 {
     $this->dateTimeRange = new \DatePeriod($start, $end->diff($start), $end);
     return $this;
 }