【问题标题】:Laravel Eloquent Carbon Date only gets one result from subHoursLaravel Eloquent Carbon Date 仅从 subHours 获得一个结果
【发布时间】:2016-10-24 00:09:53
【问题描述】:

我想从一个选定的日期(例如 10 小时前)获取所有结果,但它只会得到一个结果,我只能使用它来获得更多结果。

$sub10 = Carbon::yesterday(); // This works


$current_time = Carbon::now();
$sub10h = $current_time->subHours(10); // This doesn't

$transactionsFrom = Transaction::whereUserId($user_id)->whereDate('created_at', '>=', $sub10h)->get();

【问题讨论】:

  • 你在$sub10h有合适的时间吗?
  • @JhonnyWalker "date": "2016-06-21 23:35:02" ,是的,时间应该是对的,我已经尝试过 subHours(24) ,但与昨天()我得到更多的地方
  • 在这里出局。但是尝试Carbon::now(-10) 可能是subHours 方法搞砸了

标签: laravel date time eloquent php-carbon


【解决方案1】:

->subHour(10) 应该可以工作。你正在使用->subHours(10)

【讨论】:

    【解决方案2】:

    由于您的粒度是 小时 而不是 ,因此您应该这样做

    $transactionsFrom = Transaction::whereUserId($user_id)->where('created_at', '>=', $sub10h->toDateTimeString())->get();
    

    注意 where 而不是 whereDate 并使用 toDateTimeString() 将 Carbon 实例转换为字符串。

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 2019-06-25
      • 2013-07-29
      • 1970-01-01
      相关资源
      最近更新 更多