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

yi0zb3m4  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(285)

我目前在一个拉拉维尔项目工作。现在我需要在几天内从数据库中获取一些数据。我需要从前端传递一个参数,比如day=1或day=2。如果day=1,那么我需要取今天的邮件。如果day=2,我需要取昨天和今天的帖子。我尝试了curdate()和now()以及interval,但没有得到任何解决方案。请帮我做这个。
获取数据的代码
用于获取数据的表

bpsygsoo

bpsygsoo1#

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

相关问题