1. Go to this page and download the library: Download alexxiy/php-time-intervals library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alexxiy / php-time-intervals example snippets
use Alexxiy\TimeIntervals\IntervalFormatter;
echo (new IntervalFormatter())->inWords(new DateTimeImmutable('2010-01-10 23:05:00'));
use Alexxiy\TimeIntervals\IntervalFormatter;
$formatter = new IntervalFormatter();
echo $formatter->inWords(new DateTimeImmutable('-3 days')); // "3 days ago"
echo $formatter->inWords(new DateTimeImmutable('+3 days')); // "in 3 days"
// Against an explicit second date rather than the current time.
echo $formatter->inWords(
new DateTimeImmutable('2022-07-01 15:00:00'),
new DateTimeImmutable('2022-09-01 14:00:00'),
); // "2 months ago"
use Alexxiy\TimeIntervals\Tense;
echo $formatter->inWords($date, tense: Tense::Plain); // "3 days"
foreach (Locale::cases() as $locale) {
printf('<option value="%s">%s</option>', $locale->value, $locale->label());
}
$english = new IntervalFormatter(tense: Tense::Plain);
$polish = $english->withLocale(Locale::Polish);
$ru = IntervalFormatter::forLocale(Locale::Russian);
$ru->inWords($twoDaysAgo); // "2 дня назад"
$ru->inWords($fiveDaysAgo); // "5 дней назад"
$ru->inWords($twentyOneDaysAgo); // "21 день назад"
echo $formatter->never(); // "Never"
use Alexxiy\TimeIntervals\IntervalFormatter;
use Alexxiy\TimeIntervals\Language;
use Alexxiy\TimeIntervals\PluralForms;
use Alexxiy\TimeIntervals\PluralRule;
use Alexxiy\TimeIntervals\TenseSet;
use Alexxiy\TimeIntervals\TenseVocabulary;
use Alexxiy\TimeIntervals\Vocabulary;
final class Signed implements Language
{
public function vocabulary(): Vocabulary
{
return new Vocabulary(
plurals: PluralRule::OtherOnly,
full: new TenseSet(
past: new TenseVocabulary(
lessThanAMinute: '-0m',
oneMinute: '-1m',
minutes: new PluralForms('-%sm'),
aboutOneHour: '-1h',
hours: new PluralForms('-%sh'),
aboutOneDay: '-1d',
days: new PluralForms('-%sd'),
aboutOneMonth: '-1mo',
months: new PluralForms('-%smo'),
aboutOneYear: '-1y',
years: new PluralForms('-%sy'),
),
future: new TenseVocabulary(/* +0m, +1m, +%sm, ... */),
plain: new TenseVocabulary(/* 0m, 1m, %sm, ... */),
),
never: '-',
);
}
}
$signed = new IntervalFormatter(new Signed());
echo $signed->inWords(new DateTimeImmutable('-5 days')); // "-5d"
echo $signed->inWords(new DateTimeImmutable('+5 days')); // "+5d"
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.