knplabs/knp-time-bundle
Composer 安装命令:
composer require knplabs/knp-time-bundle
包简介
Making your dates and durations look sensible and descriptive
README 文档
README
Friendly ago/until dates ("5 minutes ago" or "in 5 minutes") and durations ("2 mins")!
Last edited: {{ post.updatedAt|time_diff }} <!-- Last edited: 1 week ago -->
Event date: {{ event.date|time_diff }} <!-- Event date: in two weeks -->
Read time: {{ post.readTimeInSeconds|duration }} <!-- Read time: 2 minutes -->
Age: {{ user.birthdate|age }} <!-- Age: 30 years old -->
Want to see it used in a screencast 🎥? Check out SymfonyCasts: https://symfonycasts.com/screencast/symfony-doctrine/ago
The formatted date/duration can be translated into any language, and many are supported out of the box.
Installation
Use Composer to install the library:
composer require knplabs/knp-time-bundle
Woo! You did it! Assuming your project uses Symfony Flex, the
bundle should be configured and ready to go. If not, you
can enable Knp\Bundle\TimeBundle\KnpTimeBundle manually.
Usage
Twig
Time formatting:
{{ someDateTimeVariable|time_diff }} {# 2 weeks ago #}
{# |ago is an alias for |time_diff #}
{{ someDateTimeVariable|ago }} {# 1 second ago #}
{# ... or use the equivalent function: #}
{{ time_diff(someDateTimeVariable) }} {# in 2 months #}
Note: the
time_difffilter/function andagoalias works fine for dates in the future, too.
Duration formatting:
{{ someDurationInSeconds|duration }} {# 2 minutes #}
Age formatting:
{# with filter: #} Age: {{ user.birthdate|age }} {# Age: 30 years old #} {# ... or use the equivalent function: #} Age: {{ age(user.birthdate) }} {# Age: 30 years old #}
Service
You can also format dates and durations in your services/controllers by autowiring/injecting the
Knp\Bundle\TimeBundle\DateTimeFormatter service:
use Knp\Bundle\TimeBundle\DateTimeFormatter; // ... public function yourAction(DateTimeFormatter $dateTimeFormatter) { $someDate = new \DateTimeImmutable('-2 years'); // or $entity->publishedDate() $toDate = new \DateTimeImmutable('now'); $agoTime = $dateTimeFormatter->formatDiff($someDate, $toDate); // $toDate parameter is optional and defaults to "now" $readTime = $dateTimeFormatter->formatDuration(64); // or $entity->readTimeInSeconds() $ageTime = $dateTimeFormatter->formatAge($someDate, $toDate); // $toDate parameter is optional and defaults to "now" return $this->json([ // ... 'published_at' => $agoTime, // 2 years ago 'read_time' => $readTime, // 1 minute // ... ]); }
Controlling the Translation Locale
The bundle will automatically use the current locale when translating the "time_diff" ("ago") and "duration" messages. However, you can override the locale:
{{ someDateTimeVariable|time_diff(locale='es') }}
{{ someDurationInSeconds|duration(locale='es') }}
{{ someDateTimeVariable|age(locale='es') }}
Tests
If you want to run tests, please check that you have installed dev dependencies.
./vendor/bin/phpunit
Maintainers
Anyone can contribute to this repository (and it's warmly welcomed!). The following people maintain and can merge into this library:
knplabs/knp-time-bundle 适用场景与选型建议
knplabs/knp-time-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.52M 次下载、GitHub Stars 达 624, 最近一次更新时间为 2012 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「knp」 「knplabs」 「bundle」 「time」 「date」 「descriptive time」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 knplabs/knp-time-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 knplabs/knp-time-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 knplabs/knp-time-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configure your knp menus with yaml files accross your project bundles
KnpMenu for CakePHP 3
KNP Menu Bundle integrations with Superdesk Publisher.
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
统计信息
- 总下载量: 9.52M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 633
- 点击次数: 33
- 依赖项目数: 43
- 推荐数: 4
其他信息
- 授权协议: MIT
- 更新时间: 2012-01-12