本文整理汇总了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();
}
示例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;
}
示例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);
}
示例4: date_diff
function date_diff(DateTimeInterface $datetime, DateTimeInterface $datetime2, bool $absolute = false)
{
return $datetime->diff($datetime2, $absolute);
}
示例5: timeRange
public function timeRange(\DateTimeInterface $start, \DateTimeInterface $end)
{
$this->dateTimeRange = new \DatePeriod($start, $end->diff($start), $end);
return $this;
}