Laravel:如何分别获得日期范围内的所有日期和日期范围内的所有日期?

问题描述 投票:0回答:1

我尝试获取在[[n日期范围之间的所有日期,以及这些日期范围内的所有日期。

控制器:

public function index() { $startDate = new Carbon('2019-11-20'); $endDate = new Carbon('2019-11-27'); $all_dates = array(); while ($startDate->lte($endDate)) { $all_dates[] = $startDate->toDateString(); $startDate->addDay(); } return $all_dates; }

我的输出看起来不错。这正是我所需要的:

["2019-11-20","2019-11-21","2019-11-22","2019-11-23","2019-11-24","2019-11-25","2019-11-26","2019-11-27"]

我尝试了不同的方式将日期范围从数据库传递到函数。但这不起作用。在我的表格中有两个重要字段:

$query = DB::select('start_date', 'end_date')->get();

1:如何获得与示例相同的结果?

2:如何在输出中忽略每个日期范围的开始日期和结束日期?我只需要一个日期范围内的日期即可。

谢谢!

laravel eloquent date-range php-carbon
1个回答
0
投票
第一个问题。

使用whereverween方法验证一列值是在两个值之间。

$from = date('2019-11-10'); $to = date('2019-11-15'); Order::whereBetween('order_from', [$from, $to])->get(); // consider from / to as start date/ end date

对于第二个问题,您可以使用whereDate获取介于开始日期和结束日期之间的所有日期! 

Order::whereDate('created_at', '>', $date_from) ->whereDate('created_at', '<', $date_to) ->pluck('created_at');

© www.soinside.com 2019 - 2024. All rights reserved.