jmf/time

时间包,用于检索和测量时间。提供PSR-20时钟接口的实现。

1.0.0 2024-05-20 02:22 UTC

This package is auto-updated.

Last update: 2024-09-20 02:59:22 UTC


README

安装与需求

使用 Composer 安装 Composer

composer require jmf/time

用法

时钟

Clock 允许检索当前时间和日期。

将其注入到应用程序中可以简化与时间相关的操作的测试(模拟其 ClockInterface 接口并使其返回值可预测)。

<?php

use Jmf\Time\Clock;

$clock = new Clock();

// Will output something like "2020-01-23"
echo $clock->getDateTime()->format('Y-m-d');

// Will output something like "2020-01-23"
echo $clock->getDateString();

// Will output something like "15:16:17"
echo $clock->getTimeString();

// Will output something like "2020-01-23 15:16:17"
echo $clock->getDateTimeString();

// Will output something like "123456789"
echo $clock->getTimestamp();

// Will output something like "123456789.0123"
echo $clock->getMicrotime();

计时器

Timer 允许测量经过的时间(以秒为单位,具有微秒级精度)。它可以启动($timer->start())、停止($timer->stop())、重置($timer->reset())和重新启动($timer->restart())。您还可以查询经过的时间($timer->getElapsed())。

<?php

use Jmf\Time\Timer;

$timer = new Timer();

sleep(1);

// Will output something like "0.0"
echo $timer->getElapsed();

$timer->start();

sleep(1);

// Will output something like "1.0023456"
echo $timer->getElapsed();

sleep(1);

$timer->stop();

sleep(1);

// Will output something like "2.0034567"
echo $timer->getElapsed();

$timer->restart();

sleep(1);

// Will output something like "1.0023456"
echo $timer->getElapsed();

sleep(1);

// Will output something like "2.0034567"
echo $timer->getElapsed();

$timer->reset();

sleep(1);

// Will output something like "0.0"
echo $timer->getElapsed();