承接 philiprehberger/php-human-duration 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

philiprehberger/php-human-duration

Composer 安装命令:

composer require philiprehberger/php-human-duration

包简介

Convert seconds into human-readable duration strings

README 文档

README

Tests Latest Version on Packagist Last updated

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:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 philiprehberger/php-human-duration 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 58
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-13