Skip to content

alisqi/twig-ext-intl

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Twig Intl Extension

Introduction

This Twig extension provides a number of filters to format dates, times, country and currency information according to given locale and language settings.

Filters

AlisQI fork

This repository was forked from twigphp/intl-extra in order to make format_date, format_time and format_datetime use the 'prototype' values of IntlDateFormatter when IntlExtension is instantiated with a custom formatter.

See: twigphp/Twig#3568

Using pretty formatting

In order to use the format_datetime_pretty and format_date_pretty filters, you must supply the IntlExtension constructor with a Closure that returns the 'pretty' formatted date. This way you can apply your opinionated format and any custom translations if your application is not yet on PHP8.

Here is an example Closure that you can use (note that the IntlDateFormatter::RELATIVE_* constants are only available on PHP8 and will cause an exception if used in PHP7.4 or older.)

/**
 * Same day                 "today at 1:37 PM"
 * Yesterday                "yesterday at 1:37 PM"
 * Within the last 7 days   "Monday", "Tuesday", etc
 * Older than 7 days        $dateFormat default
 * In the future            $dateFormat default
 *
 * If the date somehow cannot be resolved otherwise, will return default formatting for IntlDateFormatter argument.
 */
$closure = function (\DateTimeInterface $dateTime, \IntlDateFormatter $formatter) 
{
    $date = new \DateTimeImmutable($dateTime->format('Y-m-d'));
    $now = new \DateTimeImmutable('today');
    $daysAgo = (int)$date->diff($now)->format('%r%a');

    // past week: "Thursday"
    if ($daysAgo > 1 && $daysAgo < 7) {
        return (new \IntlDateFormatter(
            $formatter->getLocale(), IntlDateFormatter::NONE,  IntlDateFormatter::NONE,
            $formatter->getTimeZone(), $formatter->getCalendar(), 'EEEE'
        ))->format($dateTime);
    }

    // today or yesterday with time
    if ($daysAgo === 0 || $daysAgo === 1) {
        return (new \IntlDateFormatter(
            $formatter->getLocale(),  IntlDateFormatter::RELATIVE_SHORT, IntlDateFormatter::NONE,
            $formatter->getTimeZone(), $formatter->getCalendar(), ''
        ))->format($dateTime);
    }

    // default formatting without time
    return (new \IntlDateFormatter(
        $formatter->getLocale(), $formatter->getDateType(), IntlDateFormatter::NONE,
        $formatter->getTimeZone(), $formatter->getCalendar(), $formatter->getPattern()
    ))->format($dateTime);
};

// usage
$intlExtension = new IntlExtension(
    new IntlExtension(
        new \IntlDateFormatter(
            'nl',
            \IntlDateFormatter::FULL,
            \IntlDateFormatter::FULL,
            new \DateTimeZone('Europe/Amsterdam')
        ),
        new \NumberFormatter('nl', \NumberFormatter::DECIMAL),
        $closure
    );
);

Packages

No packages published

Languages

  • PHP 100.0%