PHP code example of alexxiy / php-time-intervals

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"

$plain = new IntervalFormatter(tense: Tense::Plain);
$plain = $formatter->withTense(Tense::Plain);

echo $plain->inWords(new DateTimeImmutable('-2 hours'));   // "2 hours"
echo $plain->inWords(new DateTimeImmutable('+2 hours'));   // "2 hours"

use Alexxiy\TimeIntervals\Style;

$fiveDaysAgo = new DateTimeImmutable('-5 days');

echo $formatter->inWords($fiveDaysAgo, style: Style::Short);   // "5d ago"

$short = new IntervalFormatter(style: Style::Short);
$short = $formatter->withStyle(Style::Short);

$short->inWords(new DateTimeImmutable('-5 days'));   // "5d ago"
$short->inWords(new DateTimeImmutable('+5 days'));   // "in 5d"

use Alexxiy\TimeIntervals\IntervalFormatter;
use Alexxiy\TimeIntervals\Locale;

echo IntervalFormatter::forLocale(Locale::Russian)->inWords($date);   // "3 дня назад"
echo IntervalFormatter::forLocale(Locale::German)->inWords($date);    // "vor 3 Tagen"

$locale = Locale::tryFrom($request->getLocale()) ?? Locale::English;

echo IntervalFormatter::forLocale($locale)->inWords($date);

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