【问题标题】:Get data of todays and 2 days before added from database从数据库中获取今天和添加前 2 天的数据
【发布时间】:2018-04-11 06:09:31
【问题描述】:

我目前正在做一个 Laravel 项目。现在我需要在几天内从数据库中获取一些数据。我需要从前端传递一个参数,比如 day =1 或 day =2。如果 day = 1,那么我需要获取今天的帖子。如果 day = 2 我需要获取昨天和今天的帖子。我尝试使用 CURDATE() 和 NOW() 和 INTERVAL,但我没有得到任何解决方案。 请帮我做这件事。

Code to fetch the data

Table used to fetch data

【问题讨论】:

  • 嗨 Gowtham,更新你的表(数据库)。
  • 嗨 Arun,我将如何更新我的表格
  • 请先阅读How to Ask。与您的问题相关的所有代码都直接属于其中,而不仅仅是转储到外部网站或作为图像。

标签: php mysql laravel-5


【解决方案1】:

If you pass day = 1 then you can get today's post by whereDate laravel function.
e.g. pass day = 1
$today = date('Y-m-d');
$post = DB::table('post')->whereDate('post_date', $today)->get();
If you pass day = 2 then first of all you have to get previous date and get current date by date function and using whereBetween laravel function you can get yesterday and today's post.
e.g. pass day = 2
$from = Yesterday date
$to   = today date
$post = POST::whereBetween('post_date', [$from, $to])->get();

【讨论】:

  • 我在使用 whereBetween 时遇到问题,我只收到昨天的帖子。我需要今天的帖子和昨天的帖子。
  • 你必须在 whereBetween 中通过昨天和今天的日期。
  • $from = date("Y-m-d",strtotime("-$day days")); $to = $today = date("Y-m-d");
  • pinned_scribbles.created_at 是日期时间还是只有日期字段?请检查您的 $from 和 $to 数据,并通过查询中传递的静态 from 和 to date 进行检查。
  • 您只需要比较 pinned_scribbles.created_at 字段中的日期。或者您必须在昨天的 00:00:00 和今天的 23:59:00 联系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多