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

Timer 是一个用于处理时间的库,特别适用于超过 24 小时的时间

2.2.1 2016-06-21 19:42 UTC

This package is auto-updated.

Last update: 2024-06-13 16:05:57 UTC


README

#WallaceMaxters\Timer 库

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

有时我们需要处理小时,超过 PHP 中的 24 小时,但 DateTime 类或其他日期函数没有令人满意的解决方案;

因此开发了 Timer 库。

使用它,您可以非常简单地进行时间操作

use WallaceMaxters\Timer\Time;

$time = Time::create(0, 0, 10);

$time->format('%h:%i%s'); // '00:00:10'

$time->addSeconds(30);

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

$time->addSeconds(-50);

$time->format('%h:%i%s'); // '-00:00:10'

如果需要,您也可以使用 Collection 类,以方便对时间进行一些操作。

use WallaceMaxters\Timer\Collection;

$collection = new Collection;

$collection[] = Time::create(0, 10, 0);

$collection[] = Time::createFromString('10 seconds');

$collection[] = Time::createFromFormat('%h:%i', '00:50');

$collection->sum(); // new Time(0, 11, 0);

$collection->min(); // new time(0, 0, 10); 

超过 24 小时时间的示例

// DateTime
$date = DateTime::createFromFormat('25:00:00');

var_dump($date->format('H:i:s')); // 01:00:00

// WallaceMaxters\Timer\Timer

$time = Time::createFromFormat('%h:%i:%s', '26:00:00');

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