dcarbone/gotime
Composer 安装命令:
composer require dcarbone/gotime
包简介
Golang-like time class(es) for PHP 8.2+
README 文档
README
Golang-inspired time helpers for PHP 8.2+.
Installation
composer require dcarbone/gotime
Quick start
<?php use DCarbone\Go\Time; $t = Time::Now(); $d = Time::ParseDuration('1h30m'); echo $t, PHP_EOL; // formatted timestamp echo $d, PHP_EOL; // 1h30m0s echo $d->Nanoseconds(), PHP_EOL; // 5400000000000
API overview
This package exposes a static facade class, DCarbone\Go\Time, plus several value objects under DCarbone\Go\Time\*.
Duration (DCarbone\Go\Time\Duration)
Duration stores elapsed time as a signed integer count of nanoseconds.
Common construction patterns:
use DCarbone\Go\Time; $a = new Time\Duration(5 * Time::Second); $b = Time::ParseDuration('5s'); $c = Time::Duration('1h2m3.5s'); // cast helper
Supported ParseDuration() units:
nsus,µs,μsmssmh
Key methods:
Nanoseconds(),Microseconds(),Milliseconds(),Seconds(),Minutes(),Hours()Truncate(Duration $m),Round(Duration $m)Compare(Duration $other)(-1,0,1)DateInterval()/IntervalSpec()__toString()(Go-like formatting, e.g.500ms,1h2m3.004s)jsonSerialize()(raw nanosecond integer)
Time (DCarbone\Go\Time\Time)
Time\Time extends \DateTime and adds Go-style helpers.
Primary constructors:
use DCarbone\Go\Time; $zero = Time::New(); // Unix epoch $now = Time::Now(); // current time
Comparison helpers:
Before(Time $t),After(Time $t),Equal(Time $t)BeforeDateTime(\DateTimeInterface $dt),AfterDateTime(...),EqualDateTime(...)
Unix helpers:
Unix()UnixNano()UnixNanoDuration()
Date/time part helpers:
Year(),Month(),Day(),Weekday()Hour(),Minute(),Second(),Nanosecond()IsZero()
Duration arithmetic:
AddDuration(Duration $d)SubDuration(Duration $d)
Static facade helpers (DCarbone\Go\Time)
- Constants:
Nanosecond,Microsecond,Millisecond,Second,Minute,Hour - Constructors and now/epoch:
New()Now()
- Duration operations:
ParseDuration(string $s)Duration(null|string|int|float|Duration|\DateInterval $input)CompareDuration(Duration $d1, Duration $d2)ZeroDuration()
- Relative time:
Since(Time\Time $t)SinceDateTime(\DateTimeInterface $dt)Until(Time\Time $t)UntilDateTime(\DateTimeInterface $dt)
Supporting value types
Time\Month(month ordinal/name helpers)Time\Weekday(weekday ordinal/name helpers)Time\IntervalSpec(intermediate duration-to-interval representation)Time\DateInterval(extends\DateInterval, including fractional seconds support)
Notes
- Precision in PHP runtime is microseconds; nanosecond APIs are represented as integer nanoseconds derived from microsecond resolution.
Duration(\DateInterval $input)converts years/months using average-hour constants, so calendar-month/year conversion is approximate.
Running tests
./vendor/bin/phpunit --display-deprecations --display-phpunit-deprecations
dcarbone/gotime 适用场景与选型建议
dcarbone/gotime 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 148.94k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dcarbone/gotime 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dcarbone/gotime 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 148.94k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 28
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-12-11