当前位置: 首页>>代码示例>>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;未经允许,请勿转载。