cocur/human-date

将日期转换为可读形式

v0.1 2014-05-14 20:44 UTC

This package is auto-updated.

Last update: 2024-08-23 16:02:02 UTC


README

将日期转换为可读格式。

Latest Stable Version Build Status Code Coverage

功能

  • 将日期转换为可读格式
  • 支持可翻译字符串
  • 无外部依赖。
  • PSR-4 兼容。
  • 兼容 PHP >= 5.4 和 HHVM
  • Symfony2Twig 的集成。

安装

您可以使用 Composer 安装 cocur/human-date

$ composer require cocur/human-date:@stable

在生产环境中,您应将 @stable 替换为您要使用的 版本

用法

您可以将一个 DateTime 实例传递给 HumanDate::transform() 方法。例如,假设今天是 2012-08-18

use Cocur\HumanDate\HumanDate;

$humanDate = new HumanDate();

echo $humanDate->transform(new DateTime('now'));
// 'Today'

echo $humanDate->transform(new DateTime('+1 day'));
// 'Tomorrow'

echo $humanDate->transform(new DateTime('-1 day'));
// 'Yesterday'

echo $humanDate->transform(new DateTime('2012-08-21'));
// 'Next Tuesday'

echo $humanDate->transform(new DateTime('2012-09-30'));
// 'September 30'

echo $humanDate->transform(new DateTime('2013-03-30'));
// 'March 30, 2013'

翻译

HumanDate 支持字符串翻译。构造函数 Cocur\HumanDate\HumanDate 接受一个 Cocur\HumanDate\Translation\TranslationInterface 实例。

$translation = MyTranslation(); // must implement `Cocur\HumanDate\Translation\TranslationInterface`
$humanDate = new HumanDate($translation);

echo $humanDate->transform(new DateTime('now'));
// Calls MyTranslation::trans()

此外,该库还包括一个用于 Symfony Translation 组件的适配器。

桥梁

cocur/human-date 包含对 Symfony 和 Twig 的桥梁。

Symfony

Symfony 桥接器为您提供了捆绑包和扩展,以便在应用程序中使用 HumanDate 作为服务。

# app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Cocur\HumanDate\Bridge\Symfony\CocurHumanDateBundle(),
        );
        // ...
    }

    // ...
}

现在您可以在应用程序的任何地方使用 cocur_human_date 服务,例如,在您的控制器中

$slug = $this->get('cocur_human_date')->slugify(new DateTime('2014-04-14'));

该捆绑包还提供了一个 cocur_human_date 服务的别名 human_date

$slug = $this->get('human_date')->slugify(new DateTime('2014-04-14'));

翻译

HumanDate 包含对 Symfony Translation 组件的适配器。该适配器需要一个 Symfony\Component\Translation\TranslatorInterface 实例,并接受一个翻译域和区域。适配器的 trans() 方法将这些值传递给 Symfony\Component\Translation\TranslatorInterface::trans() 的每次调用。

use Cocur\HumanDate\Bridge\Symfony\Translation\SymfonyTranslation;
use Cocur\HumanDate\HumanDate;

// Get or create an instance of Symfony\Component\Translation\TranslatorInterface
// For example, inside a controller
$sfTrans = $this->get('translation');

// Create an adapter with translation domain "human_date" and locale "en"
// trans() passes domain and locale to every call of Symfony\Component\Translation\TranslatorInterface::trans()
// If you omit the domain and locale it uses the defaults.
$trans = new SymfonyTranslation($sfTrans, 'human_date', 'en');

$humanDate = new HumanDate($trans);

Twig

如果您使用 Symfony2 框架与 Twig,则在设置好 Symfony2 集成(见上文)后,可以在模板中使用 Twig 过滤器 humanDate

{{ post.createdAt|humanDate }}

如果您在 Symfony2 框架之外使用 Twig,您首先需要将扩展添加到您的环境中

use Cocur\HumanDate\Bridge\Twig\HumanDateExtension;
use Cocur\HumanDate\HumanDate;

$twig = new Twig_Environment($loader);
$twig->addExtension(new HumanDateExtension(new HumanDate()));

您可以在 Twig 文档 中找到有关注册扩展的更多信息。

变更日志

版本 0.1 (2014 年 5 月 14 日)

  • 初始版本(从 BraincraftedHumanDateBundle 转移而来)

作者

许可证

MIT 许可证(MIT)版权所有 (c) 2012 Florian Eckerstorfer

特此授予任何获得此软件及其相关文档副本(“软件”)的人免费使用软件的权利,不受限制,包括但不限于以下权利:使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许提供软件的人这样做,前提是满足以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按照“原样”提供,不提供任何形式的质量保证,无论是明示的还是暗示的,包括但不限于适用性、特定用途的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是在合同行为、侵权行为或其他行为中,无论是由软件本身、使用软件或其他与软件相关的行为引起的。