salernolabs/relative-time
, (*1)
Library for building relative time strings in PHP., (*2)
Usage
Installation
First include this project with composer, (*3)
composer require salernolabs/relative-time
Then run the formatter on a DateTime object., (*4)
$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"., (*5)
This library will also give short-hand conversational outputs for some values. For example:, (*6)
Time Modification |
Output |
-5 minutes |
just now |
+5 minutes |
momentarily |
-1 minute |
about a minute ago |
+1 minute |
about a minute from now |
-1 hour |
about an hour ago |
+1 hour |
about an hour from now |
-1 day |
yesterday |
+1 day |
tomorrow |
And actual numbers for other times, for example:, (*7)
Time Modification |
Output |
-45 seconds |
45 seconds ago |
-45 minutes |
45 minutes ago |
-7 hours |
7 hours ago |
-3 years |
3 years ago |
You can easily wrap the output in an html5 time tag by using the getRelativeTimeTag function., (*8)
$formatter = new \SalernoLabs\RelativeTime\Formatter();
$relativeTime = $formatter->getRelativeTimeTag(new \DateTime('-47 minutes'));
echo $relativeTime;
This would output:, (*9)
<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., (*10)
$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
Input Time |
Output |
1:00 |
one o'clock |
5:03 |
five o'clock, |
2:07 |
almost ten after two o'clock, |
3:10 |
ten after three o'clock |
4:15 |
quarter after four o'clock |
6:22 |
almost half past six o'clock |
7:32 |
half past seven o'clock |
8:44 |
almost a quarter to nine o'clock |
9:45 |
a quarter to ten o'clock |
11:55 |
almost twelve o'clock |
12:00 |
twelve o'clock, |
12:59 |
almost one o'clock |
12:45 |
a quarter to one o'clock |
12:43 |
almost a quarter to one o'clock |