本文整理汇总了PHP中Drupal\Core\Datetime\DateFormatterInterface::formatTimeDiffUntil方法的典型用法代码示例。如果您正苦于以下问题:PHP DateFormatterInterface::formatTimeDiffUntil方法的具体用法?PHP DateFormatterInterface::formatTimeDiffUntil怎么用?PHP DateFormatterInterface::formatTimeDiffUntil使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Datetime\DateFormatterInterface
的用法示例。
在下文中一共展示了DateFormatterInterface::formatTimeDiffUntil方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: formatTimestamp
/**
* Formats a timestamp.
*
* @param int $timestamp
* A UNIX timestamp to format.
*
* @return string
* The formatted timestamp string using the past or future format setting.
*/
protected function formatTimestamp($timestamp)
{
$granularity = $this->getSetting('granularity');
$options = ['granularity' => $granularity];
if ($this->request->server->get('REQUEST_TIME') > $timestamp) {
return SafeMarkup::format($this->getSetting('past_format'), ['@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, $options)]);
} else {
return SafeMarkup::format($this->getSetting('future_format'), ['@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, $options)]);
}
}
示例2: formatTimestamp
/**
* Formats a timestamp.
*
* @param int $timestamp
* A UNIX timestamp to format.
*
* @return array
* The formatted timestamp string using the past or future format setting.
*/
protected function formatTimestamp($timestamp)
{
$granularity = $this->getSetting('granularity');
$options = ['granularity' => $granularity, 'return_as_object' => TRUE];
if ($this->request->server->get('REQUEST_TIME') > $timestamp) {
$result = $this->dateFormatter->formatTimeDiffSince($timestamp, $options);
$build = ['#markup' => SafeMarkup::format($this->getSetting('past_format'), ['@interval' => $result->getString()])];
} else {
$result = $this->dateFormatter->formatTimeDiffUntil($timestamp, $options);
$build = ['#markup' => SafeMarkup::format($this->getSetting('future_format'), ['@interval' => $result->getString()])];
}
CacheableMetadata::createFromObject($result)->applyTo($build);
return $build;
}
示例3: render
/**
* {@inheritdoc}
*/
public function render(ResultRow $values)
{
$value = $this->getValue($values);
$format = $this->options['date_format'];
if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'))) {
$custom_format = $this->options['custom_date_format'];
}
if ($value) {
$timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
$time_diff = REQUEST_TIME - $value;
// will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
switch ($format) {
case 'raw time ago':
return $this->dateFormatter->formatTimeDiffSince($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2));
case 'time ago':
return $this->t('%time ago', array('%time' => $this->dateFormatter->formatTimeDiffSince($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2))));
case 'raw time hence':
return $this->dateFormatter->formatTimeDiffUntil($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2));
case 'time hence':
return $this->t('%time hence', array('%time' => $this->dateFormatter->formatTimeDiffUntil($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2))));
case 'raw time span':
return ($time_diff < 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2));
case 'inverse time span':
return ($time_diff > 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2));
case 'time span':
$time = $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2));
return $time_diff < 0 ? $this->t('%time hence', array('%time' => $time)) : $this->t('%time ago', array('%time' => $time));
case 'custom':
if ($custom_format == 'r') {
return format_date($value, $format, $custom_format, $timezone, 'en');
}
return format_date($value, $format, $custom_format, $timezone);
default:
return format_date($value, $format, '', $timezone);
}
}
}