How to format a period of time with IntlDateFormatter in PHP

275 Views Asked by At

I would like to output a formatted period of time for different locales, e.g.: de_DE: 25. - 28. August 2022 es_ES: 25-28 de agosto de 2022

So far I have to 'construct' this output. Example:

$locales = array(
    'de_DE',
    'en_GB',
    'fr_FR',
    'es_ES',
    'zh_CN',
    'ru_RU',
);
$date = new \DateTime('2022-08-25');
$date2 =new \DateTime('2022-08-28');
foreach ($locales as $loc){
$fmt = new IntlDateFormatter(
$loc,
IntlDateFormatter::LONG,
IntlDateFormatter::NONE,
'Europe/Berlin',
IntlDateFormatter::GREGORIAN);
    if ($loc == 'zh_CN')
    {
      $fmt->setPattern('y年M月d-');
      $part1= $fmt->format($date);
      $fmt->setPattern('d日'); 
      $part2= $fmt->format($date2);
      echo $loc.' formatierte Ausgabe:  ' .$part1.''.$part2.'| some text<br>';
    }
....

Output: 2022年8月25-28日| some text

I'm sure there must be a better way to do this? Thanks in advance!

0

There are 0 best solutions below