PHPackages                             mengshaoying/timer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mengshaoying/timer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mengshaoying/timer
==================

计算时间差

1.0.0(3y ago)051MITPHPPHP ^5.3

Since Jan 17Pushed 3y ago1 watchersCompare

[ Source](https://github.com/MengShaoying/Timer)[ Packagist](https://packagist.org/packages/mengshaoying/timer)[ RSS](/packages/mengshaoying-timer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

`PHP` 计算时间差
===========

[](#php-计算时间差)

关于仓库里面的代码
---------

[](#关于仓库里面的代码)

写程序的时候可能会想计算两个时间点之间的代码执行的时间，这时就需要计算时间差了。`PHP`里面，有 `hrtime`（要求`PHP`版本大于或等于`7.3.0`）或者`microtime`函数可以用来获取当前的精确时间， 从而能计算短时间之间的间隔。这个仓库里面有个`Timer`类便是用来实现这种需求的，里面根据运行环境 自动使用`hrtime`或者`microtime`来进行时间的记录。

使用说明
----

[](#使用说明)

通过实例化`Timer`类来获得当前的时间，然后调用对象方法获得两个时间对象的时间差。示例：

```
$t0 = new Timer\Timer();
for ($i = 0; $i < 10000000; $i++) {
    $x = mt_rand();
}
$t1 = new Timer\Timer();
// 输出时间差，t1 - t0
echo $t1->sub($t0);
echo PHP_EOL;
```

示例代码输出类似：

```
0.772363515

```

可以通过运行仓库的`php index.php`来看示例效果。

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1216d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c849a19dcdd53c3de15e6767e7ef478d0727d0baa8410d55ecf413960fce7e4d?d=identicon)[MengShaoying](/maintainers/MengShaoying)

---

Top Contributors

[![MengShaoying](https://avatars.githubusercontent.com/u/16191911?v=4)](https://github.com/MengShaoying "MengShaoying (2 commits)")[![az13js](https://avatars.githubusercontent.com/u/20492037?v=4)](https://github.com/az13js "az13js (1 commits)")

### Embed Badge

![Health badge](/badges/mengshaoying-timer/health.svg)

```
[![Health](https://phpackages.com/badges/mengshaoying-timer/health.svg)](https://phpackages.com/packages/mengshaoying-timer)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
