demirkaric/php-duration-formatter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

demirkaric/php-duration-formatter

Composer 安装命令:

composer require demirkaric/php-duration-formatter

包简介

A PHP library for parsing and formatting time durations.

README 文档

README

PHPUnit Coverage Status PHP License

Advanced PHP library for parsing, formatting, converting, and humanizing time durations.

Supports intuitive input formats like 1h 30m, 01:30:00, 5400, or ISO 8601 (PT1H30M, P1DT2H) and provides custom formatting, JSON serialization, and accurate conversion to seconds and minutes.

🛠️ Built as an enhanced and modernized alternative to kevinkhill/php-duration, offering expanded feature support, token-based formatting, custom hours-per-day handling, and complete PHPUnit test coverage.

✅ Features:

  • Flexible input support: parse durations from strings, numbers, or colon-formatted time
  • ISO 8601 duration format support (parsing and formatting)
  • Conversion to seconds, minutes, arrays, strings, and JSON
  • Custom formatting with tokens (d, hh, mm, ss, etc.)
  • Support for days with customizable hoursPerDay
  • Human-readable output (1h 30m)
  • Chainable and reusable instance
  • Implements JsonSerializable and Stringable
  • Fully tested with 94+ % PHPUnit coverage

📥 Installation

Install via Composer:

composer require demirkaric/php-duration-formatter

🚀 Usage

✅ Basic Usage

use Demirk\PhpDurationFormatter\TimeDuration;

$duration = new TimeDuration('1h 42m 30s');

echo $duration->toSeconds();      // 6150.0
echo $duration->toMinutes();      // 102.5
echo $duration->humanize();       // "1h 42m 30s"
echo $duration->format();         // "01:42:30" (default format)
echo (string) $duration;          // "01:42:30"

echo json_encode($duration);      // {"seconds":6150,"values":{"days":0,"hours":1,"minutes":42,"seconds":30},"formatted":"01:42:30","humanized":"1h 42m 30s"}

🧩 Supported Input Formats

  • "1h 30m"
  • "1d 4h 5m 2.5s"
  • "01:30" or "01:30:45"
  • 3600 or 3661.5
  • "2d"
  • ISO 8601 durations: "PT1H30M", "P1DT2H", "P2W"

🌐 ISO 8601 Duration Support

The library fully supports ISO 8601 duration format for both parsing and formatting.

Parsing ISO 8601 Durations

use Demirk\PhpDurationFormatter\TimeDuration;

// Time only
$duration1 = new TimeDuration('PT1H30M');
echo $duration1->toSeconds();    // 5400.0
echo $duration1->humanize();     // "1h 30m"

// Date and time
$duration2 = new TimeDuration('P1DT2H30M');
echo $duration2->toSeconds();    // 95400.0
echo $duration2->humanize();     // "1d 2h 30m"

// Weeks (converted to days)
$duration3 = new TimeDuration('P2W');
echo $duration3->toSeconds();    // 1209600.0
echo $duration3->humanize();     // "14d"

// Decimal values
$duration4 = new TimeDuration('PT2.5H');
echo $duration4->humanize();     // "2h 30m"

Formatting to ISO 8601

$duration = new TimeDuration('1d 2h 30m 45s');
echo $duration->toIso8601();     // "P1DT2H30M45S"

$duration2 = new TimeDuration('5h 30m');
echo $duration2->toIso8601();    // "PT5H30M"

Supported ISO 8601 Components:

  • P - Period designator (required)
  • nD - Days
  • T - Time designator
  • nH - Hours
  • nM - Minutes
  • nS - Seconds
  • nW - Weeks (converted to days)

💡 Note: Weeks are normalized to days (e.g., P1W becomes P7D when formatting).

🧠 Custom Format Tokens

Use format(string $pattern) to generate custom formatted strings.

Token Meaning Example
d Days (non-padded) 1
dd Days (zero-padded) 01
h Hours (non-padded) 2
hh Hours (zero-padded) 02
H Total hours (including days) 26
HH Total hours (zero-padded) 26
m Minutes 5
mm Minutes (zero-padded) 05
s Seconds 4.5
ss Seconds (zero-padded) 04.5
S Rounded seconds 4
SS Rounded seconds (zero-padded) 04
$duration = new TimeDuration('1d 2h 5m 30s');

echo $duration->format('dd hh:mm:ss'); // 01 02:05:30
echo $duration->format('H:mm');        // 26:05

⚠️ format() will throw an exception if both d and H are used together (conflict between relative and absolute hours).

🔄 Conversion Methods

$duration->toSeconds();                // float/int
$duration->toSeconds('1h 5s');         // pass string directly
$duration->toMinutes();                // in float
$duration->toMinutes(null, 0);         // rounded to int
$duration->toMinutes(null, 2);         // rounded to 2 decimal places

📚 Humanized Output

$duration = new TimeDuration('2d 3h 15m');

echo $duration->humanize(); // "2d 3h 15m"

🧪 Tests

This library is tested with PHPUnit:

  • ✅ Covers parsing, formatting, rounding, edge cases, and serialization

To run tests:

vendor/bin/phpunit

👏 Credits

This library is inspired by and originally based on Kevin Hill’s php-duration, with significant improvements in architecture, extensibility, and formatting capabilities.
Parsing, formatting, and conversion logic has been modernized and tested.

📄 License

MIT © Demir Karić
See LICENSE file for details.

demirkaric/php-duration-formatter 适用场景与选型建议

demirkaric/php-duration-formatter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.74k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 demirkaric/php-duration-formatter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 demirkaric/php-duration-formatter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 16.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-24