从数据库添加之前获取今天和2天的数据

时间:2018-04-11 06:09:32

标签: php mysql laravel-5

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

Code to fetch the data

Table used to fetch data

1 个答案:

答案 0 :(得分:0)

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();