當前位置: 首頁>>代碼示例>>PHP>>正文


PHP DateTimeInterface::diff方法代碼示例

本文整理匯總了PHP中DateTimeInterface::diff方法的典型用法代碼示例。如果您正苦於以下問題:PHP DateTimeInterface::diff方法的具體用法?PHP DateTimeInterface::diff怎麽用?PHP DateTimeInterface::diff使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在DateTimeInterface的用法示例。


在下文中一共展示了DateTimeInterface::diff方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: formatDiff

 /**
  * 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();
 }
開發者ID:KnpLabs,項目名稱:KnpTimeBundle,代碼行數:20,代碼來源:DateTimeFormatter.php

示例2: compareDuration

 /**
  * 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;
 }
開發者ID:schpill,項目名稱:thin,代碼行數:20,代碼來源:Period.php

示例3: toRelative

 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);
 }
開發者ID:rtens,項目名稱:ucdi,代碼行數:32,代碼來源:Bootstrapper.php

示例4: date_diff

function date_diff(DateTimeInterface $datetime, DateTimeInterface $datetime2, bool $absolute = false)
{
    return $datetime->diff($datetime2, $absolute);
}
開發者ID:badlamer,項目名稱:hhvm,代碼行數:4,代碼來源:datetime_funcs.php

示例5: timeRange

 public function timeRange(\DateTimeInterface $start, \DateTimeInterface $end)
 {
     $this->dateTimeRange = new \DatePeriod($start, $end->diff($start), $end);
     return $this;
 }
開發者ID:gallna,項目名稱:media-library,代碼行數:5,代碼來源:EpgContainer.php


注:本文中的DateTimeInterface::diff方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。