salernolabs / relative-time
Library for building relative time strings in PHP.
Installs: 2 678
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.2
Requires (Dev)
- phpunit/phpunit: 6.5
README
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