A long journey to formatting a date internationally in PHP

2017-03-20: xBazilio has rewritten my PHP-CPP extension as a PHP7 native, give it a try, repo is the same — https://github.com/ksimka/intl_dtpg.

TL;DR At the moment you can't simply format a date internationally without a year. The best solution so far — a bunch of config files with prepared patterns for every locale you need.

Let's start with a simple problem: format a date internationally: there must be a day, a full month name and a year. It's a really easy one. PHP has its i18n extension called intl which is a part of PHP core since 5.3 or so. And intl has IntlDateFormatter class. We will use its LONG format.

<?php

foreach (['en_US', 'ru_RU', 'es_ES', 'fa_IR'] as $locale) {  
    $formatter = new IntlDateFormatter(
        $locale, 
        IntlDateFormatter::LONG, 
        IntlDateFormatter::NONE, 
        'Europe/Moscow'
    );
    echo $formatter->format(1455111783), PHP_EOL;
}

Output:

February 10, 2016  
10 февраля 2016 г.  
10 de febrero de 2016  
۱۰ ﻑﻭﺭیﻩٔ ۲۰۱۶ ﻡ. // this is actually RTL text, but I'm not sure this blog can handle it properly

So far so good. Now let's slightly change the initial conditions: format a date internationally: there must be a day and a full month name.