Name of day/month that's not today in another language in php

6.6k views Asked by At

Im trying to get the name of a day in php in another language than English. I changed the locale settings to setlocale(LC_ALL, 'nl_NL'); (this returns true) but the date still shows in English. I then found out you should use strftime() for it to work, but that just returns the current time in the set language, and I'm trying to loop trough a number of predefined days, so the output would be

Vrijdag 30 augustus

Zaterdag 31 augustus

Zondag 1 september

etc.

Subtracting/adding days from the current timestamp is also not an option for what I want to do.

3

There are 3 answers

0
Haim Evgi On BEST ANSWER

Whenever you need to manipulate date/time stamps based on locale, you should use strftime

also change the encoding to utf-8

http://php.net/manual/en/function.strftime.php

example :

<?php
header('Content-Type: text/html; charset=UTF-8');

$myDate = "Feb 21, 2013";

$locale = 'fr_FR.utf8';
setlocale(LC_ALL, $locale);
echo strftime('%d %B %Y', strtotime($myDate));  

$locale = 'en_US.utf8';
setlocale(LC_ALL, $locale);
echo strftime('%d %B %Y', strtotime($myDate));
?>
0
virtualmarc On

The function strftime takes a second parameter: a unix timestamp:

https://www.php.net/manual/en/function.strftime.php

You can simply convert your dates to a unix timestamp and pass them as second parameter.

0
celsowm On

strftime was DEPRECATED in PHP 8.1

You can use IntlDateFormatter:

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

echo $formatter->formatObject(new \DateTime(), "eeee dd MMMM", "nl_NL");

Result:

woensdag 21 september

  • note that "eeee" is a format from ICU