英文:
search for records created from one date to another date
问题
我需要搜索从一日期到另一日期创建的记录。
$users = DB::table('users')
->whereDate('created_at', '>=', '2023-06-01')
->whereDate('created_at', '<', '2023-07-01')
->get();
我制作的查询仅在特定日期上搜索我,如何在两个日期之间搜索?
从2023-06-01到2023-07-01
英文:
i need to search for records created from one date to another date.
$users = DB::table('users')
->whereDate('created_at', '2023-06-01')
->get();
The query that i make searches me only on a specific date, how can search between two dates?
2023-06-01 at 2023-07-01
答案1
得分: 1
如果您在查询中使用whereBetween()
,它将为您提供存在于两个日期之间的数据,但不包括日期B上的数据。然而,如果您想要包括在日期B上创建的数据,您可以使用以下SQL表达式和whereRaw()
:
whereRaw('DATE(column_name) BETWEEN DATE(?) AND DATE(?)', [$dateA, $dateB])
这样,您将获得从日期A开始到包括日期B在内的所有数据范围内的数据。请记住将'column_name'替换为您要过滤的实际列,将$dateA
和$dateB
替换为您感兴趣的具体日期。
请注意,使用whereRaw()
允许您直接编写SQL表达式,因此请确保验证和清理输入,以防止任何SQL注入漏洞。
英文:
If you're using whereBetween()
in your query, it will give you data that exists between two dates, excluding the data on date B. However, if you want to include the data created on date B as well, you can use whereRaw()
with the following SQL expression:
whereRaw('DATE(column_name) BETWEEN DATE(?) AND DATE(?)', [$dateA, $dateB])
This way, you'll get all the data that falls within the range starting from date A up to and including date B. Remember to replace 'column_name' with the actual column you want to filter and $dateA
and $dateB
with the specific dates you are interested in.
Keep in mind that using whereRaw()
allows you to directly write SQL expressions, so make sure to validate and sanitize the input to prevent any SQL injection vulnerabilities.
答案2
得分: 1
你可以使用 "whereBetween" 函数来完成:
use Carbon\Carbon;
$startDate = Carbon::createFromFormat('Y-m-d', '2016-06-01')->startOfDay();
$endDate = $startDate->addMonth(1);
$users = DB::table('users')
->whereBetween('created_at', [$startDate, $endDate])
->get();
英文:
You can do it using "whereBetween" function
use Carbon\Carbon;
$startDate = Carbon::createFromFormat('Y-m-d', '2016-06-01')->startOfDay();
$endDate = $startDate->addMonth(1);
$users = DB::table('users')
->whereBetween('created_at', [$startDate, $endDate])
->get();
答案3
得分: 0
你可以使用以下方式使用 whereBetween
来完成:
$startDate = '2023-06-01';
$endDate = '2023-07-01';
$users = DB::table('users')
->whereBetween('created_at', [$startDate, $endDate])
->get();
英文:
You can do it using whereBetween
in the following way:
$startDate = '2023-06-01';
$endDate = '2023-07-01';
$users = DB::table('users')
->whereBetween('created_at', [$startDate, $endDate])
->get();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论