ramphor/date-human-readable

为PHP DateTime对象生成可读字符串

1.0.0 2020-11-02 02:19 UTC

This package is auto-updated.

Last update: 2024-08-29 05:31:15 UTC


README

为PHP DateTime对象生成可读字符串。它处理过去和未来的日期。对于未来的日期,它使用格式'In x 单位',例如:'In 1 分钟'。对于过去的日期,它使用'x 单位前',例如:'2 年前'。

注意:对于相差超过一天的时间比较,使用Unix时间戳之间的差异。

安装

如果你使用Composer管理依赖项,你可以在你的composer.json文件中包含以下内容

"require": {
    "ramphor/date-human-readable": "~1.0.0"
}

否则,你可以直接要求文件

require_once 'path/to/date-human-readable/src/HumanReadable.php';

使用

use Ramphor\Date\HumanReadable;

HumanReadable::parse(new DateTime('now'));         // Moments ago
HumanReadable::parse(new DateTime('+ 59 second')); // Seconds from now
HumanReadable::parse(new DateTime('+ 1 minute'));  // In 1 minute
HumanReadable::parse(new DateTime('- 59 minute')); // 59 minutes ago

// You can supply a secondary argument to provide an alternate reference
// DateTime. The default is the current DateTime, ie: DateTime('now'). In
// addition, it takes into account the day of each DateTime. So in the next
// two examples, even though they're only a second apart, 'Yesterday' and
// 'Tomorrow' will be displayed

$now = new DateTime('1991-05-18 00:00:00 UTC');
$dateTime = new DateTime('1991-05-17 23:59:59 UTC');
HumanReadable::parse($dateTime, $now); // Yesterday

$now = new DateTime('1991-05-17 23:59:59 UTC');
$dateTime = new DateTime('1991-05-18 00:00:00 UTC');
HumanReadable::parse($dateTime, $now) // Tomorrow

测试

Build Status

从项目目录中,可以使用phpunit运行测试

许可证

在MIT许可证下发布 - 有关详细信息,请参阅LICENSE.txt