PHPackages                             zhusaidong/php-timeago - 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. zhusaidong/php-timeago

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

zhusaidong/php-timeago
======================

php timeago library

00

Since Jun 17Compare

[ Source](https://github.com/zhusaidong/timeago.php)[ Packagist](https://packagist.org/packages/zhusaidong/php-timeago)[ RSS](/packages/zhusaidong-php-timeago/feed)WikiDiscussions Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

timeago.php
===========

[](#timeagophp)

timeago.php 是一个语言化之前时间的php库。

使用方法
====

[](#使用方法)

安装
--

[](#安装)

> composer require zhusaidong/php-timeago

使用
--

[](#使用)

```
use zhusaidong\TimeAgo\TimeAgo;

$timeAgo = new TimeAgo;

$time = time() - random_int(1, 999) ** 2;

echo 'now: ' . date('Y-m-d H:i:s') . PHP_EOL;
echo 'time: ' . date('Y-m-d H:i:s', $time) . PHP_EOL;
echo 'timeAgo: ' . $timeAgo->setTimestamp($time)->format() . PHP_EOL;
```

注册本地化语言
-------

[](#注册本地化语言)

默认的语言是 en, 目前内置的语言包括 en 和 zh-cn，你也可以通过`$timeAgo->registerLocales()`来注册自己的本地化语言或者个性化语言。

```
//register
$timeAgo->registerLocales('testLocal', [
    'second' => (new TimeGroup)
                ->set(new Timeslot(0, 30, '不久前'))
                ->set(new Timeslot(31, 60, '%t秒以前')),
    'minute' => '%t分钟以前',
    'hour'   => (new TimeGroup)
                ->set(new Timeslot(0, 12, '半天前'))
                ->set(new Timeslot(13, 24, '%t小时以前')),
    'day'    => new Locales('%t天以前'),
    'week'   => new Locales('%t周以前'),
    'month'  => new Locales('%t个月以前'),
    'year'   => new Locales('%t年以前'),
]);

//use it
$timeAgo->setTimestamp($time)->format('testLocal');
```

语言包
---

[](#语言包)

注册本地化语言包时，可以使用多种方式

### 字符串

[](#字符串)

> 'minute' =&gt; '%t分钟以前',

### `Locales`

[](#locales)

针对有单复数的语言，如英语，

> 'hour' =&gt; new Locales('an hour ago', '%t hours ago'),

### `TimeGroup` + `Timeslot`

[](#timegroup--timeslot)

针对有时间段的需求，如

> 'second' =&gt; (new TimeGroup) -&gt;set(new Timeslot(0, 30, '不久前')) -&gt;set(new Timeslot(31, 60, '%t秒以前')),

###  Health Score

16

—

LowBetter than 4% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3039961?v=4)[zhusaidong](/maintainers/zhusaidong)[@zhusaidong](https://github.com/zhusaidong)

### Embed Badge

![Health badge](/badges/zhusaidong-php-timeago/health.svg)

```
[![Health](https://phpackages.com/badges/zhusaidong-php-timeago/health.svg)](https://phpackages.com/packages/zhusaidong-php-timeago)
```

###  Alternatives

[sas/variant-switch

Show a variant switch within the listing and cart

181.2k](/packages/sas-variant-switch)

PHPackages © 2026

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