定制 salavert/time-ago-in-words 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

salavert/time-ago-in-words

最新稳定版本:v1.7.1

Composer 安装命令:

composer require salavert/time-ago-in-words

包简介

Time ago Twig extension where you can easily convert a datetime or timestamp to a distance of time in words.

README 文档

README

Packagist Build Status

This is a Twig extension for Symfony Framework where you can easily convert a datetime/timestamp to a distance of time in words.

By example

{{ user.lastLogin | time_ago_in_words }}

Outputs 3 days ago

Installation for Symfony

  1. Update your composer.json

    • Symfony 2/3 with Twig 1/2

      "require": {
          "salavert/time-ago-in-words": "1.*"
      }

      or use composer's require command: composer require salavert/time-ago-in-words:1.*

    • Symfony 4 with Twig 2/3

      "require": {
          "salavert/time-ago-in-words": "2.*"
      }

      or use composer's require command: composer require salavert/time-ago-in-words:2.*

  2. Register an Extension as a Service

Now you must let the Service Container know about your newly created Twig Extension:

YAML:

# app/config/config.yml
services:
    salavert.twig.time_ago:
        class: Salavert\Twig\Extension\TimeAgoExtension
        arguments: ["@translator"]
        tags:
            - { name: twig.extension }

XML:

# or into your bundle src\Acme\AcmeBundle\Resources\config\services.xml
<service id="salavert.twig.time_ago" class="Salavert\Twig\Extension\TimeAgoExtension">
    <tag name="twig.extension" />
    <argument type="service" id="translator" />
</service>

Usage

To display distance of time in words between a date and current date:

{{ message.created | time_ago_in_words }}

To display distance of time between two custom dates you should use

{{ message.created | distance_of_time_in_words(message.updated) }}

You also have two available options, for both time_ago_in_words & distance_of_time_in_words filters

  • include_seconds (boolean) if you need more detailed seconds approximations if time is less than a minute
  • include_months (boolean) if you want days to be approximated in months if time is greater than 31 days.

Thus, if you want to have the months approximation but not the seconds one, you should use:

{{ message.created | time_ago_in_words(false, true) }}

Translations

Add the following translations to your

  • \app\Resources\translations\messages.locale.yml or
  • \translations\messages.locale.yml

This is a translation to spanish:

# 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
1 day ago: hace 1 día
%days days ago: hace %days días
"{1} 1 month ago |]1,Inf[ %months months ago": "{1} hace un mes |]1,Inf[ hace %months meses"
"{1} 1 year ago |]1,Inf[ %years years ago":  "hace un año |]1,Inf[ Hace %years años"

In the same case, for future:

# Time ago in words - Twig Extension
in less than %seconds seconds: en menos de %seconds segundos
in half a minute: en medio minuto
in less than a minute: en menos de un minuto
in 1 minute: en 1 minuto
in %minutes minutes: en %minutes minutos
in about 1 hour: dentro de casi 1 hora
in about %hours hours: en %hours horas
in 1 day: en 1 día
in %days days: en %days días

Testing

To launch all tests first make sure dependecies are met with composer and run:

./vendor/phpunit/phpunit/phpunit

salavert/time-ago-in-words 适用场景与选型建议

salavert/time-ago-in-words 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 618.83k 次下载、GitHub Stars 达 90, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「twig」 「time」 「time ago」 「distance of time」 「time ago in words」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 salavert/time-ago-in-words 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 salavert/time-ago-in-words 我们能提供哪些服务?
定制开发 / 二次开发

基于 salavert/time-ago-in-words 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 618.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 93
  • 点击次数: 23
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 90
  • Watchers: 6
  • Forks: 31
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知