phplegends/timespan

此包已被放弃,不再维护。作者建议使用 wallacemaxters/timespan 包。

时间段库

1.0.0 2021-12-22 01:22 UTC

This package is auto-updated.

Last update: 2024-02-12 15:34:24 UTC


README

这是一个仅用于PHP中处理时间段的库。

使用它可以非常简单地处理时间

use PHPLegends\Timespan\Timespan;

$time = new Timespan(0, 0, 10);

echo $time->format(); // '00:00:10'

$time->addSeconds(30);

echo $time->format(); // '00:00:40'

$time->addSeconds(-50);

echo $time->format(); // '-00:00:10'

$time->addMinutes(2);

echo $time->format('%i minutes %s seconds');  // '1 minutes 50 seconds'

时间段示例

$time = Timespan::createFromFormat(
    Timespan::DEFAULT_FORMAT, 
    '26:00:00'
);

echo $time->format(); // '26:00:00'

要从DateTime Diff创建时间段,可以使用 Timespan::createFromDateDiff

$timespan = Timespan::createFromDateDiff(
    new DateTime('2021-01-01 23:00:00'),
    new DateTime('2021-01-03 02:00:00')
);

echo $timespan->format(); // '27:00:00'

可用的时间格式字符

字符 描述
%h 小时值。
%i 分钟值。范围从 `0` 到 `59`。
%I 总分钟值。
%s 秒值。范围从 `0` 到 `59`。