mkusher/time-ago-in-words
这是一个Twig扩展,您可以轻松地将日期时间或时间戳转换为时间距离的文字描述。
v1.1
2014-02-20 13:53 UTC
Requires
- php: >=5.3.0
- sonata-project/intl-bundle: 2.*
- twig/twig: ~1.12
This package is not auto-updated.
Last update: 2024-09-24 05:47:53 UTC
README
这是一个Symfony2框架的Twig扩展,您可以轻松地将日期时间戳转换为时间距离的文字描述。
示例
{{ user.lastLogin|ago }}
输出示例
half a minute ago
3 minutes ago
about 1 hour ago
Today at 04:14 AM
Yersterday at 17:36 PM
18 Feb at 3:26 PM
22 Dec 2013
Symfony2的安装
- 更新您的composer.json文件
{
"require": {
"mkusher/time-ago-in-words": "dev-master"
}
}
或使用composer的require命令
composer require mkusher/time-ago-in-words:1.*
- 注册扩展为服务
现在您必须让服务容器知道您新创建的Twig扩展
YAML
# app/config/config.yml
services:
mkusher.twig.time_ago:
class: Mkusher\Twig\Extension\TimeAgoExtension
arguments: [@translator, @sonata.intl.templating.helper.datetime]
tags:
- { name: twig.extension }
XML
# or into your bundle src\Acme\AcmeBundle\Resources\config\services.xml
<service id="mkusher.twig.time_ago" class="Mkusher\Twig\Extension\TimeAgoExtension">
<tag name="twig.extension" />
<argument type="service" id="translator" />
<argument type="service" id="sonata.intl.templating.helper.datetime" />
</service>
并更新您的AppKernel.php文件
<?php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Sonata\IntlBundle\SonataIntlBundle(),
// ...
);
}
要配置SonataIntlBundle,请遵循说明
用法
要显示日期和当前日期之间的时间距离
{{ message.created|ago }}
要显示两个自定义日期之间的时间距离,您应该使用
{{ message.created|distance_of_time_in_words(message.updated) }}
您还有两个可用的选项,适用于both ago & distance_of_time_in_words过滤器
- include_seconds (布尔值) 如果您需要更详细的时间小于一分钟的秒近似值
因此,如果您不希望有秒近似值,您应该使用
{{ message.created|ago(false) }}
翻译
将以下翻译添加到您的\app\Resources\translations\messages.locale.yml
这是西班牙语的翻译
# Time ago in words - Twig Extension
less than %seconds seconds ago: hace menos de %seconds segundos
half a minute ago: hace medio minuto
less than a minute ago: hace menos de un minuto
1 minute ago: hace 1 minuto
%minutes minutes ago: hace %minutes minutos
about 1 hour ago: hace casi 1 hora
about %hours hours ago: hace %hours horas
today at %time: today at %time
yersterday at %time: yersterday at %time
%date at %time: %date at %time
%date: %date