【问题标题】:Get records within a specific timeframe in Laravel在 Laravel 中获取特定时间范围内的记录
【发布时间】:2017-06-10 01:58:18
【问题描述】:

在我的网站上,我使用 Laravel 的 created_at 来存储图片上传的时间。

但是,我希望能够创建一个仅在特定时间范围内获取图像的查询。

那些是:

-过去 24 小时

-过去一周

-过去一个月

-路径年份

我在想……

$images = Images::where('created_at', '>=', Carbon::now()->hours(24))->get();

从特定时间获取记录很容易,因为我可以简单地从 24 到 168 再到 672 到 2087(日、周、月、年)。

想法?

【问题讨论】:

    标签: php mysql sql laravel laravel-5.1


    【解决方案1】:

    使用sub 方法:

    Images::where('created_at', '>=', Carbon::now()->subDay())->get();
    Images::where('created_at', '>=', Carbon::now()->subWeek())->get();
    Images::where('created_at', '>=', Carbon::now()->subMonth())->get();
    Images::where('created_at', '>=', Carbon::now()->subYear())->get();
    

    或者:

    Images::where('created_at', '>=', Carbon::now()->subHours(24))->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 2021-04-17
      • 2013-09-12
      • 1970-01-01
      • 2020-11-30
      相关资源
      最近更新 更多