danieldtjules/php-pretty-datetime

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

1.0.0 2018-08-23 06:05 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:53:59 UTC


README

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

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

安装

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

"require": {
    "danielstjules/php-pretty-datetime": "~1.0.0"
}

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

require_once 'path/to/php-pretty-datetime/src/PrettyDateTime.php';

使用方法

use PrettyDateTime\PrettyDateTime;

PrettyDateTime::parse(new DateTime('now'));         // Moments ago
PrettyDateTime::parse(new DateTime('+ 59 second')); // Seconds from now
PrettyDateTime::parse(new DateTime('+ 1 minute'));  // In 1 minute
PrettyDateTime::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');
PrettyDateTime::parse($dateTime, $now); // Yesterday

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

测试

Build Status

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

许可协议

在 MIT 许可协议下发布 - 详细信息请见 LICENSE.txt