get name of the day in local language

Viewed 2852

I need the name of the day in a local (serbian) language.

$date = date_create();
$day = date_format($date,"l");
switch ($day) {
    case 'Monday': $day = "PONEDELJAK"; break;
    case 'Tuesday': $day = "UTORAK"; break;
    case 'Wednesday': $day = "SREDA"; break;
    case 'Thursday': $day = "ČETVRTAK"; break;
    case 'Friday': $day = "PETAK"; break;
    case 'Saturday': $day = "SUBOTA"; break;
    case 'Sunday': $day = "NEDELJA"; break;
}
$date = $day . ' - ' . date_format($date,"d. m. Y.");
echo $date;

Result - PETAK - 14. 07. 2017.

So, it works, but I suppose there is a shorter way, probably using setlocale(LC_TIME, 'sr_BA') but I'm not sure where to put it.

Any help?

3 Answers

strftime was DEPRECATED in PHP 8.1

You can use IntlDateFormatter:

$formatter = new \IntlDateFormatter(
    'sr_BA',
    \IntlDateFormatter::LONG,
    \IntlDateFormatter::LONG,
    'Europe/Sarajevo' //more in: https://www.php.net/manual/en/timezones.europe.php
);

echo $formatter->formatObject(new \DateTime(), "eeee", "sr_BA");
  • note that "eeee" is a format from ICU
Related