【问题标题】:Laravel - Carbon - Errors parsing datetime to extract monthLaravel - Carbon - 解析日期时间以提取月份时出错
【发布时间】:2018-01-20 16:14:49
【问题描述】:

在这里需要一些帮助。 让我说这可能很容易回答,但我不是程序员,并且在没有学习 php 基础知识的情况下开始使用 laravel 做一个应用程序。

所以,我使用 Carbon 来解析日期时间,提取其中的月份,但我遇到了一些错误。 这是我要解析的内容:

 DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at') 

我在查询中这样做,如下所示:

->where(Carbon::parse(DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at'))->month,'=', $december)

我得到的错误是:

Exception in Carbon.php line 291:
DateTime::__construct(): Failed to parse time string (["2017-12-07 10:03:12",....

做了一些搜索,发现问题是“”,我不得不修剪字符串,像这样:

where(Carbon::parse(trim(DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at'),'"'))->month,'=', $december)

... 并得到同样的错误。 所以,我已经在视图中将查询隔离到 dd() ,但我仍然无法理解出了什么问题。 如果我在视图中添加这个

$test = DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at');

我明白了:

Collection {#460 ▼
  #items: array:657 [▼
    0 => "2017-12-07 10:03:12"
    1 => "2017-12-07 10:03:46"
    2 => "2017-12-07 10:04:20"
    3 => "2017-12-07 10:05:08"
    4 => "2017-12-07 10:06:29"
    5 => "2017-12-07 10:07:39"
    6 => "2017-12-07 10:09:58"
    7 => "2017-12-07 10:13:25"
    8 => "2017-12-07 10:13:57"
    9 => "2017-12-07 10:14:28"
    10 => "2017-12-07 10:15:10".....

如果我 dd 这个:

$test = trim(DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at'),'"');

我明白了:

"["2017-12-07 10:03:12","2017-12-07 10:03:46","2017-12-07 10:04:20","2017-12-07 10:05:08","2017-12-07 10:06:29","2017-12-07 10:07:39","2017-12-07 10:09:58","2017 ▶"

有什么线索吗?

提前致谢

【问题讨论】:

  • 这就是我想要做的。还是谢谢

标签: php laravel parsing php-carbon


【解决方案1】:

您可以在查询中执行此操作。请改用whereMonth() 方法:

->whereMonth('created_at', 12)

【讨论】:

  • 非常感谢!出于某种原因,我认为 whereMonth() 仅适用于日期,而不适用于日期时间。它让我的代码更简单并解决了问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 2021-08-27
  • 1970-01-01
相关资源
最近更新 更多