philiprehberger/php-human-duration
Composer 安装命令:
composer require philiprehberger/php-human-duration
包简介
Convert seconds into human-readable duration strings
README 文档
README
Convert seconds into human-readable duration strings.
Requirements
- PHP 8.2+
Installation
composer require philiprehberger/php-human-duration
Usage
Creating Durations
use PhilipRehberger\HumanDuration\Duration; // From seconds $duration = Duration::fromSeconds(3665); // From minutes $duration = Duration::fromMinutes(90); // From hours $duration = Duration::fromHours(2.5); // From the difference between two dates $duration = Duration::between( new DateTimeImmutable('2026-01-01 08:00:00'), new DateTimeImmutable('2026-01-01 09:30:00'), );
Parsing Durations
use PhilipRehberger\HumanDuration\Duration; // Shorthand $duration = Duration::parse('1h 30m 15s'); // 5415 seconds // Verbose $duration = Duration::parse('2 hours, 45 minutes'); // 9900 seconds // Decimal $duration = Duration::parse('1.5h'); // 5400 seconds // With days $duration = Duration::parse('1d 2h'); // 93600 seconds // ISO 8601 $duration = Duration::fromIso('PT1H30M15S'); // 5415 seconds $duration = Duration::fromIso('P1DT2H'); // 93600 seconds // Convert to ISO 8601 $duration->toIso(); // "PT1H30M15S"
Formatting Durations
$duration = Duration::fromSeconds(3665); $duration->toHuman(); // "1h 1m 5s" $duration->toVerbose(); // "1 hour, 1 minute, 5 seconds" $duration->toCompact(); // "1:01:05" // Stringable — casting to string uses toHuman() echo $duration; // "1h 1m 5s"
Accessing Raw Values
$duration = Duration::fromSeconds(5400); $duration->totalSeconds(); // 5400 $duration->totalMinutes(); // 90.0 $duration->totalHours(); // 1.5
Comparing and Combining Durations
$a = Duration::fromMinutes(90); $b = Duration::fromHours(1); $a->isGreaterThan($b); // true $a->isLessThan($b); // false $a->equals($b); // false $a->add($b)->toHuman(); // "2h 30m" $a->subtract($b)->toHuman(); // "30m"
Proportional Calculations
$duration = Duration::fromMinutes(90); // Get 25% of a duration $quarter = $duration->percentage(25); $quarter->totalSeconds(); // 1350 (22.5 minutes) // Get a fraction of a duration $third = $duration->fraction(1, 3); $third->totalSeconds(); // 1800 (30 minutes)
Negative Durations
$duration = Duration::fromSeconds(-65); $duration->toHuman(); // "-1m 5s" $duration->toVerbose(); // "-1 minute, 5 seconds" $duration->toCompact(); // "-1:05"
API
| Method | Return | Description |
|---|---|---|
Duration::fromSeconds(int $seconds) |
Duration |
Create from seconds |
Duration::fromMinutes(int|float $minutes) |
Duration |
Create from minutes |
Duration::fromHours(int|float $hours) |
Duration |
Create from hours |
Duration::between(DateTimeInterface $start, DateTimeInterface $end) |
Duration |
Create from date difference |
Duration::parse(string $input) |
Duration |
Parse human-readable string (e.g., "1h 30m", "2 hours") |
Duration::fromIso(string $iso) |
Duration |
Parse ISO 8601 duration (e.g., "PT1H30M") |
toHuman() |
string |
Compact format: 1h 5m 30s |
toVerbose() |
string |
Verbose format: 1 hour, 5 minutes, 30 seconds |
toCompact() |
string |
Clock format: 1:05:30 |
toIso() |
string |
ISO 8601 format: PT1H5M30S |
totalSeconds() |
int |
Total seconds |
totalMinutes() |
float |
Total minutes |
totalHours() |
float |
Total hours |
totalDays() |
float |
Total days |
isGreaterThan(Duration $other) |
bool |
Check if greater than another duration |
isLessThan(Duration $other) |
bool |
Check if less than another duration |
equals(Duration $other) |
bool |
Check if equal to another duration |
add(Duration $other) |
Duration |
Add two durations, returning a new instance |
subtract(Duration $other) |
Duration |
Subtract a duration, returning a new instance |
percentage(float $percent) |
Duration |
Return given percentage of this duration |
fraction(int $numerator, int $denominator) |
Duration |
Return proportional fraction of this duration |
Development
composer install vendor/bin/phpunit vendor/bin/pint --test vendor/bin/phpstan analyse
Support
If you find this project useful:
License
philiprehberger/php-human-duration 适用场景与选型建议
philiprehberger/php-human-duration 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「time」 「human」 「format」 「duration」 「readable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 philiprehberger/php-human-duration 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 philiprehberger/php-human-duration 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 philiprehberger/php-human-duration 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a simple decimal/roman number converter
Softonic OpenApi validation formats extension
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
Date component is a set of methods to help with the manipulation of dates.
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-13