【问题标题】:Carbon get month index from month nameCarbon 从月份名称中获取月份索引
【发布时间】:2018-03-01 22:17:40
【问题描述】:

我想使用 Carbon 从月份名称中获取月份索引。

但我使用土耳其月份名称。

我将查询字符串传递给像这样?ay=Temmuz&yıl=2017 这样的索引,因此在这种情况下碳应该给出 7。

我的索引函数中的相关部分是这样的:

public function index()
{
    $gonderiler = Gonderi::latest();

    if ($ay = request('ay'))
    {
        Carbon::setLocale(config('app.locale'));
        $gonderiler->whereMonth('created_at', Carbon::parse($ay)->month);
    }

    if ($yil = request('yil'))
    {
        $gonderiler->whereYear('created_at', $yil);
    }

    / ... / 

}

当我单击侧栏并传递此查询字符串时,它会给出如下错误消息:

"DateTime::__construct(): 解析时间字符串 (Temmuz) 失败 位置 0 (T):在数据库中找不到时区"

【问题讨论】:

  • 你使用的服务器是什么?
  • 我在 64 位 Ubuntu 机器上使用 vallet。带有 Laravel 框架 5.5.3 的 Ubuntu 16.04 LTS

标签: php laravel laravel-5 php-carbon


【解决方案1】:

在 Linux 上

如果您在翻译时遇到问题,请检查系统中安装的语言环境(本地和生产环境)。

locale -a to list locales enabled.
sudo locale-gen tr_TR.UTF-8 to install a new locale.
sudo dpkg-reconfigure locales to publish all locale enabled.

然后重新启动您的系统。见文档:http://carbon.nesbot.com/docs/#api-localization

更多可以参考:Laravel Carbon localization not working (get localized name of month from number)

因为你得到的错误是关于Timezone not found in database.

【讨论】:

  • 语言环境已安装。这个错误信息是 Laravel 的异常错误信息。如果我单击侧栏并将此查询字符串传递给索引方法。像那样gunluk.dev/?ay=Temmuz&yıl=2017
  • Carbon 没有月份名称解析功能。
猜你喜欢
  • 1970-01-01
  • 2011-03-12
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 2015-11-29
  • 1970-01-01
相关资源
最近更新 更多