php-pretty-datetime

Generates human-readable strings for PHP DateTime objects

  • 所有者: danielstjules/php-pretty-datetime
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

php-pretty-datetime

Generates human-readable strings for PHP DateTime objects. It handles dates in the past and future. For future dates, it uses the format 'In x unit', ie: 'In 1 minute'. For dates in the past, it uses 'x unit ago', ie: '2 years ago'.

Note: Comparison of dates, for those beyond a day apart, uses the difference between their Unix timestamps.

Installation

If you're using Composer to manage dependencies, you can include the following
in your composer.json file:

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

Otherwise, you can simply require the file directly:

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

Usage

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

Tests

Build Status

From the project directory, tests can be ran using phpunit

License

Released under the MIT License - see LICENSE.txt for details.

主要指标

概览
名称与所有者danielstjules/php-pretty-datetime
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2013-07-03 03:59:19
推送于2024-02-03 09:10:45
最后一次提交2018-08-22 23:05:47
发布数1
最新版本名称1.0.0 (发布于 )
第一版名称1.0.0 (发布于 )
用户参与
星数58
关注者数4
派生数9
提交数21
已启用问题?
问题数3
打开的问题数0
拉请求数1
打开的拉请求数1
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?