1

I am trying to change the language of the outputted date from date() in CodeIgniter.

I have put all the required files in application/language/swedish/* and tried setting the variable $config['language'] = 'swedish';. However date('F', strtotime('2017-07-01')); still outputs July and not Juli.

Philip
  • 137
  • 1
  • 2
  • 16
  • 1
    Use `strftime`. – Chibueze Opata Sep 04 '17 at 12:45
  • 1
    Check this [Change the language for date in php](https://stackoverflow.com/questions/6910912/change-the-language-for-date-in-php) – Nana Partykar Sep 04 '17 at 12:45
  • It's only 12 items in an array that is needed to str_replace it. Personally I would not spend the time to fix that. I'd just make a translation table. Or even better, just use `$array_months[date("n")]` – Andreas Sep 04 '17 at 13:27

2 Answers2

1

This solution works for a CodeIgniter site hosted on an Ubuntu server.

  1. Check if locale is installed and available on the server by running locale -a in the terminal. (If your locale is in the list jump to point 3)
  2. Install desired locale by running sudo apt-get install language-pack-XX. (Replace XX with your language. se for swedish. List of all available packs)
  3. Add setlocale(LC_ALL, 'sv_SE.utf8'); to app/application/config/config.php (Switch to desired locale. List of all locales)
  4. Use strftime('%B', ...) instead of date('F', ...)
Philip
  • 137
  • 1
  • 2
  • 16
-1

As mentioned in comments above it's very easy to translate manually.
First item in array has to be empty since arrays start with 0 and months start with 1.
Or you have to subtract one from date() but either way this looks in the array for the numeric month and echos the value (month).

$array_months = ["empty", "januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"];

$input = "2017-04-04";
Echo $array_months[date("n", strtotime($input))];

https://3v4l.org/MDLPO

Andreas
  • 23,610
  • 6
  • 30
  • 62