2010-09-16 17:15
浏览 70


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?

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

图片转代码服务由CSDN问答提供 功能建议


   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'dymy':$ format ='%d。%m。%Y';  break; 
返回strftime($ format,strtotime($ time)); 

wher $ time = 2010-12 -03 10:00:00 。 问题是我的日子和月份仍然是英语。 如何将其更改为例如 挪威语?


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doufan8805 2010-09-16 17:16

    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().

    解决 无用
    打赏 举报
  • douxiawei9318 2010-09-16 17:19

    Use IntlDateFormatter::format.


    $df = new IntlDateFormatter('nb_NO',
        IntlDateFormatter::FULL, IntlDateFormatter::FULL,
    echo $df->format(time());


    torsdag 16. september 2010 kl. 21.23.03 Norge
    解决 无用
    打赏 举报
  • douyun3631 2012-08-05 15:27

    I know this topic is old but i had the same problem today. The dates are displayed correctly on my linux machine, but on my windows machine they are still in english.

    If you want this to work on linux and windows machines you have to be careful with the setlocale() function. On linux you have to use 'nb_NO' (for example) for norwegian formats and on windows you have to use 'norwegian':

    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        setlocale(LC_ALL, 'norwegian');
    } else {
        setlocale(LC_ALL, 'nb_NO');

    You can find a list of language codes for Windows in the MSDN.

    解决 无用
    打赏 举报

相关推荐 更多相似问题