salernolabs/relative-time

Library for building relative time strings in PHP.

v1.0.0 2019-08-03 21:34 UTC

This package is auto-updated.

Last update: 2024-11-04 09:11:42 UTC


README

Latest Stable Version License Build Status

Library for building relative time strings in PHP.

Usage

Installation

First include this project with composer

composer require salernolabs/relative-time

Formatting Time

Then run the formatter on a DateTime object.

$formatter = new \SalernoLabs\RelativeTime\Formatter();
$relativeTime = $formatter->getRelativeTime(new \DateTime('-47 minutes'));
echo $relativeTime;

This would output '47 minutes ago'. The formatter will also go forward as well so if you put in '+47 minutes' it will say "47 mintues from now".

Examples of Time Reformatting

This library will also give short-hand conversational outputs for some values. For example:

And actual numbers for other times, for example:

HTML5 Tags

You can easily wrap the output in an html5 time tag by using the getRelativeTimeTag function.

$formatter = new \SalernoLabs\RelativeTime\Formatter();
$relativeTime = $formatter->getRelativeTimeTag(new \DateTime('-47 minutes'));
echo $relativeTime;

This would output:

<time title="April 14th, 2017 - 4:14PM EDT">47 minutes ago</time>

Relative Time Clock

The relative time clock can give you a textual representation of a time of day.

$clock = new \SalernoLabs\RelativeTime\Clock();

$time = $clock
    ->setTime(new \DateTime('7:47'))
    ->getTime();

echo $time; // a quarter to eight o'clock

Examples of Relative Clock Output