0

The output of the following routine is exactly what I want, but as a PHP beginner, I have had a hard time to get there and I'm not convinced with the steps I took. Can I achive the same result using only one DateFormatter and/or not having to poke the format with regex? Is there a possibility for an outright format definition?

<?php
    // display: (today, Juli 12th)

    $locales=array("en-gb","en","de-ch","ru","sv","pl","ca-es","es","it","fr","nl","pt","de");

    foreach ($locales as $locale) {

        $fmt1 = new IntlDateFormatter(
            $locale,
            IntlDateFormatter::RELATIVE_FULL,   // always 'today' 
            IntlDateFormatter::NONE,            // no time
            date_default_timezone_get(),
            IntlDateFormatter::GREGORIAN
        );

        $fmt2 = new IntlDateFormatter(
            $locale,
            IntlDateFormatter::FULL,            // everything
            IntlDateFormatter::NONE,            
            date_default_timezone_get(),
            IntlDateFormatter::GREGORIAN
        );

        // get 'today' from fmt1
        $today_string = $fmt1->format(time());

        // modify pattern of fmt2:
        $date_format = datefmt_get_pattern($fmt2);
        // get 'd to MMMM' or 'MMMM to d' from pattern, drop the rest
        $regEx="@(^.*?(d.*?MMMM|MMMM.*?d).*?$)@";
        datefmt_set_pattern($fmt2, preg_replace($regEx, '$2', $date_format)); 

        // use pattern to get output:
        $day_month = $fmt2->format(time());

        // concat and output
        $my_string = $today_string . ", " . $day_month;
        echo $my_string . PHP_EOL;
    }
?>

Output:

today, 12 July
today, July 12
heute, 12. Juli
сегодня, 12 июля
i dag, 12 juli
dzisiaj, 12 lipca
avui, 12 de juliol
hoy, 12 de julio
oggi, 12 luglio
aujourd’hui, 12 juillet
vandaag, 12 juli
hoje, 12 de julho
heute, 12. Juli
jamacoe
  • 519
  • 4
  • 16

1 Answers1

0

For the "today" string i can't find something better.... only setting them with the locales in the array.

Probably something like this instead of $fmt2 could be more readable

setlocale(LC_TIME, $locale);
$my_string = $today_string . ", " . date('d F');

Actually i don't remember date() takes setLocale in consideration... I'm sure about strftime() but it is deprecated in PHP 8.1.0

Gicu Aftene
  • 502
  • 2
  • 9
  • I wrote, that the output is exactly what I want. So sometimes the month comes first (en-us), at other times the day. There might be a dot behind the day (de) or not. Also, date('d F') doesn't always translate the month. Some use capital letters for nouns, others not. All this isn't considered. And yes, I could hard code everything in the array, put I'd rather use the functions and be flexible. – jamacoe Jul 12 '22 at 16:36
  • @jamacoe So you are looking for somethig similar to a i18n for dates in order to "translate it" as the locale you are setted with the correct semantic rules... – Gicu Aftene Jul 13 '22 at 09:44
  • @jamacoe Here i found a [similar topic](https://stackoverflow.com/questions/1114488/date-function-output-in-a-local-language) but the semantic formatting based of the locale it's something hard to find already done from the backend.... Otherwise you can delegate the print of the date in the client side with javascript (if it's a web app) [look here](https://stackoverflow.com/questions/85116/display-date-time-in-users-locale-format-and-time-offset) – Gicu Aftene Jul 13 '22 at 10:13
  • I'm happy with the output as it is, so I'm using what I programmed above. The question was towards the IntlDateFormatter function and it's parameter IntlDateFormatter:: if I can define a format there directly such that I get exactly the output I have. – jamacoe Jul 13 '22 at 16:02