iserv / zeit
一款用于处理日期和时间的通用库。
v1.3
2023-07-14 14:12 UTC
Requires
- php: >=8.1
- psr/clock: ^1.0
This package is auto-updated.
Last update: 2024-09-14 16:55:42 UTC
README
用法
Zeit 和 Clock
Zeit 库为您在处理与日期和时间相关的信息方面提供了一些帮助。一方面,我们有 Zeit
工具和一个 Clock
,可以询问当前时间。它默认使用系统时钟,但在测试中可以注入一个固定值。
use IServ\Library\Zeit\Clock\FixedClock; use IServ\Library\Zeit\Zeit; $now = Zeit::now(); printf("It is %s o'clock", $now->format('H')); Zeit::setClock(FixedClock::fromString('2021-04-23 12:00:00')); $now = Zeit::now(); printf("It is %s o'clock", $now->format('H')); // Will print 12 o'clock
Clock
是 PSR-20: Clock 的扩展,增加了对时钟的 usleep
操作,以便您可以进行时间流逝测试。
日期和时间
该库还提供了 Date
和 Time
领域对象。这些对象可以用来模拟只有相关数据,而您通常会依赖于 PHP 内置的 DateTime 对象。可以将这些模型转换为 DateTime 对象以进行进一步处理,但请注意,PHP 将为您添加缺少的部分。
use IServ\Library\Zeit\Date; use IServ\Library\Zeit\Time; $date = Date::fromParts(2021, 4, 23); echo $date->getValue(); // will print the normalized string representation "2021-04-23" $time = Time::fromParts(10, 37); // The third parameter for seconds is optional and defaults to zero echo $time->getValue(); // will print the normalized string representation "10:37:00" // You can merge a Date and a Time to \DateTimeImmutable $dateTime = $date->withTime($time); echo $dateTime->format('Y:m:d H:i:s'); // will print "2021-04-23 10:37:00"
通过 ZeitBridge
组件,可以充分利用 Date
和 Time
的真正功能,该组件将库集成到您的 Doctrine/Symfony 项目中,并允许直接将 Zeit 模型映射到实体和表单中。