meklis/php-duration
Composer 安装命令:
composer require meklis/php-duration
包简介
Converts between colon formatted time, human-readable time and seconds
README 文档
README
Convert durations between colon formatted time, human-readable time and seconds
This package was created with a very specific goal in mind, to enable an easy way for users to input how long something took, as a duration of time.
The library can accept either in colon separated format, like 2:43 for 2 minutes and 43 seconds OR written as human readable or abbreviated time, such as 6m21s for 6 minutes and 21 seconds.
Both can be converted into seconds and minutes with precision for easy storage into a database.
Seconds, colon separated, abbreviated, all three can be parsed and interchanged.
- supports hours, minutes, and seconds (with microseconds)
- humanized input supports any form of the words "hour", "minute", "seconds"
- Example, you could input 1h4m2s or 4 Hr. 32 Min.
Install
composer require khill/php-duration:~1.0
Usage
use Khill\Duration\Duration; $duration = new Duration('7:31'); echo $duration->humanize(); // 7m 31s echo $duration->formatted(); // 7:31 echo $duration->toSeconds(); // 451 echo $duration->toMinutes(); // 7.5166 echo $duration->toMinutes(null, 0); // 8 echo $duration->toMinutes(null, 2); // 7.52
$duration = new Duration('1h 2m 5s'); echo $duration->humanize(); // 1h 2m 5s echo $duration->formatted(); // 1:02:05 echo $duration->toSeconds(); // 3725 echo $duration->toMinutes(); // 62.0833 echo $duration->toMinutes(null, 0); // 62
// Configured for 6 hours per day $duration = new Duration('1.5d 1.5h 2m 5s', 6); echo $duration->humanize(); // 1d 4h 32m 5s echo $duration->formatted(); // 10:32:05 echo $duration->toSeconds(); // 37925 echo $duration->toMinutes(); // 632.083333333 echo $duration->toMinutes(null, 0); // 632
$duration = new Duration('4293'); echo $duration->humanize(); // 1h 11m 33s echo $duration->formatted(); // 1:11:33 echo $duration->toSeconds(); // 4293 echo $duration->toMinutes(); // 71.55 echo $duration->toMinutes(null, 0); // 72
Note
You do not have to create a new object for each conversion, you can also pass any of the three forms into any of the methods to get the immediate output.
$duration = new Duration; echo $duration->humanize('1h 2m 5s'); // 1h 2m 5s echo $duration->formatted('1h 2m 5s'); // 1:02:05 echo $duration->toSeconds('1h 2m 5s'); // 3725 echo $duration->toMinutes('1h 2m 5s'); // 62.0833 echo $duration->toMinutes('1h 2m 5s', true); // 62
meklis/php-duration 适用场景与选型建议
meklis/php-duration 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 969 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「time」 「convert」 「duration」 「seconds」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 meklis/php-duration 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 meklis/php-duration 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 meklis/php-duration 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a simple decimal/roman number converter
Immutable, timezone-aware date and time types with Duration, Period, and Interval
Convert a value in seconds into a human-readable convenience duration
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
统计信息
- 总下载量: 969
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-23