[英]Between two(2) Dates with date format for column (Y-m-d)
So I have this code as post request that collects the $start_date
& $end_date
所以我有这段代码作为收集$start_date
& $end_date
发布请求
$start_date = date("Y-m-d",strtotime($request->start_date));
$end_date = date("Y-m-d",strtotime($request->end_date));
and I have this query to get all the records between two(2) dates 我有这个查询来获取两(2)个日期之间的所有记录
$date_time_records = DateTimeAPI::select('ACNo','datetime','state','deviceID','status')
->whereBetween('datetime', [$start_date, $end_date])
->get();
But the problem is: 但是问题是:
The field format of datetime
in mysql is (Ymd h:i:s)
mysql中datetime
的字段格式为(Ymd h:i:s)
What I'm trying to do is to match the date range and the field datetime
with the same format. 我想做的是用相同的格式匹配日期范围和字段datetime
。
Like this ('Ym-d')
像这样('Ym-d')
The select query works fine if the start_date
and end_date
formatted as (Ymd h:i:s)
. 如果start_date
和end_date
格式设置为(Ymd h:i:s)
则select查询可以正常工作。 But What I only need is (Ymd)
但是我唯一需要的是(Ymd)
change your code 更改您的代码
$start_date = date("Y-m-d H:i:s",strtotime($request->start_date));
$end_date = date("Y-m-d H:i:s",strtotime($request->end_date));
or using carbon 或使用碳
use Carbon\Carbon;
Carbon::parse($start_date)->format('Y-m-d H:i:s');
Carbon::parse($end_date)->format('Y-m-d H:i:s');
Use ->whereDateBetween()
instead on ->whereBetween()
使用->whereDateBetween()
而不是在->whereBetween()
->whereDateBetween('datetime', [$start_date, $end_date])
您也可以使用
$query->whereDate('datetime', '>=', $start_date)->whereDate('datetime', '<=', $end_date);
As you have mentioned that 就像你提到的那样
your mysql column is of format - Y-m-d h:i:s
and format in date range variable is - Y-m-d.
So, you can use DATE()
function of MYSQL and implement in you query as below 因此,您可以使用MYSQL的DATE()
函数并在查询中实现如下
$date_time_records = DateTimeAPI::select('ACNo','datetime','state','deviceID','status')
->whereBetween('DATE(datetime)', [$start_date, $end_date])
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.