3

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.

Community
  • 1
  • 1
user2729072
  • 33
  • 1
  • 3
  • 1
    careful with `LC_ALL`, there's `LC_TIME`, which is what you actually need. That, and [this question has been asked before](http://stackoverflow.com/questions/8744952/php-how-to-format-a-given-datetime-object-considering-localegetdefault) – Elias Van Ootegem Aug 29 '13 at 11:50
  • Try like `setlocale(LC_ALL,'dutch');` It works for me. But `nl_NL` doesnt work – Bora Aug 29 '13 at 11:53

3 Answers3

3

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));
?>
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
1

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
celsowm
  • 846
  • 9
  • 34
  • 59
0

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

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

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

virtualmarc
  • 533
  • 1
  • 4
  • 16