views:

65

answers:

3

I'm using the following function to retrieve date and time:

  function formatEventTime($time, $type, $locale = 'nb_NO') {
    setlocale(LC_TIME, $locale);

    switch($type) {
      case 'date' :             $format = '%d.%m'; break;
      case 'dm'   :             $format = '%d. %B'; break;
      case 'time' :             $format = '%H:%M'; break;
      case 'dmy'  :         $format = '%d.%m.%Y'; break;
    }
    return strftime($format, strtotime($time));
  }

wher $time = 2010-12-03 10:00:00. The problem is that my days and months are still in English. How do I change this to e.g. Norwegian?

Update
This works on my server, but not on my locale machine.

+4  A: 

You have to use strftime() instead of date().

Returns a string formatted according format using the given timestamp or the current local time if no timestamp is given. Month and weekday names and other language-dependent strings respect the current locale set with setlocale().

kemp
A: 

or use javascript new Date()

Fincha
A: 

Use IntlDateFormatter::format.

Example:

<?php
$df = new IntlDateFormatter('nb_NO',
    IntlDateFormatter::FULL, IntlDateFormatter::FULL,
    'Europe/Oslo');
echo $df->format(time());

gives:

torsdag 16. september 2010 kl. 21.23.03 Norge
Artefacto
Note: requires PHP 5.3 or later
kemp
Or the pecl extension [intl](http://pecl.php.net/package/intl).
Artefacto
Will this affect the entire system? Or just within the function I use it?
Steven
@Steven Unlike `setlocale`, this is thread-safe.
Artefacto