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.

AB naming

At our team we have a lot of names for pair of interacting objects.

To be more clear let's take users. In messenger it's sender and receiver, in diary it's viewer and viewed, in profile it's visitor and visited and so on.

This goes worse when one have to name variable or argument for current user. It can be current, active, simply user or something else.

Posted on
Tagged in naming

When old is better than new

When you have a large codebase with dozens of packages and thousands of classes, sooner or later comes that moment, when you have to create a newer version of something: subproject, package, class, function, etc. But you can't just remove the previous version, you must support both for some period of time. And that time is not the best time of your life, trust me.

But misfortune never comes alone. Here unexpectedly comes one of the two hardest things in computer sciencenaming things. And in my experience, most of developers choose the dark side — suffix new (or 2 or similar) for new thing.

I'll try to explain, why using old for old thing is many times better.

This blog is powered by Ghost

Ghost is just nearly awesome. In case you didn't know.

<?php

// sample bunch of code to test highlighting
$ghost = tryCodeHighlight(true, 42);
if ($ghost) {  
    $ghost->letsDance();
}
Posted on