代码之家  ›  专栏  ›  技术社区  ›  Chris

php设置输入的区域设置

  •  0
  • Chris  · 技术社区  · 6 年前

    我需要将多语言日期字符串转换为碳实例:

    英语作品好:

    new Carbon('December 2018');
    

    但在输入其他语言时失败:

    new Carbon('Dezember 2018');
    

    我可以为diffForHumans()之类的输出设置语言环境,但我还没有找到一种方法来为输入字符串设置语言环境。

        Carbon::setLocale('de');
        new Carbon('Dezember 2018');
    

    也不行。抛出:

    DateTime::__construct(): Failed to parse time string (Dezember 2018) at position 0 (D): The time  
      zone could not be found in the database  
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   parpar    6 年前

    你需要使用 createFromFormat

    Carbon::setLocale('de');
    Carbon::createFromFormat("F Y", "Dezember 2018");