使用两个日期之间的范围获取两列之间的记录,还可以使用搜索筛选条件。

huangapple go评论59阅读模式
英文:

Get records between two columns using range between two dates, Also search filter criteria

问题

**我想要获取在这个日期区间内(2022-02-15,2022-08-20)加入的记录**

    $users = User::whereDate('start_at', '>=', $startDate)
                ->whereDate('end_at', '<=', $endDate)
                ->get();
英文:

I want to get records that are joined between this date period (2022-02-15, 2022-08-20)

$users = User::whereDate(&#39;start_at&#39;, &#39;&gt;=&#39;, $startDate)
            -&gt;whereDate(&#39;end_at&#39;, &#39;&lt;=&#39;, $endDate)
            -&gt;get();

答案1

得分: 1

您可以使用Laravel的whereBetween函数:

$users = User::whereBetween('start_at', [ $startDate, $endDate])->get();

注意 $startDate$endDate必须是Carbon的实例。

$startDate = new Carbon('paste_your_start_date')->format('Y-m-d')." 00:00:00";

更新

这是从两个分开的列获取数据的一种方法。示例未经测试。

$start_at = Carbon::createFromFormat('Y-m-d H', '2022-02-15 0')->toDateString();
$end_at = Carbon::createFromFormat('Y-m-d H', '2022-08-20 0')->toDateString();

$users = User::whereRaw("start_at <= date('$start_at')")
                      ->whereRaw("end_at >= date('$end_at')")
                      ->get();
英文:

You can use the whereBetween Laravel function:

$users = User::whereBetween(&#39;start_at&#39;, [ $startDate, $endDate])-&gt;get();

Note $startDate and $endDate must be instance of Carbon.

$startDate = new Carbon(&#39;paste_your_start_date&#39;)-&gt;format(&#39;Y-m-d&#39;).&quot; 00:00:00&quot;;

Update

This would be one method to get data from two seperated columns. Example is not tested.

$start_at = Carbon::createFromFormat(&#39;Y-m-d H&#39;, &#39;2022-02-15 0&#39;)-&gt;toDateString();
$end_at = Carbon::createFromFormat(&#39;Y-m-d H&#39;, &#39;2022-08-20 0&#39;)-&gt;toDateString();

$users = User::whereRaw(&quot;start_at &lt;=  date(&#39;$start_at&#39;)&quot;)
                      -&gt;whereRaw(&quot;end_at &gt;=  date(&#39;$end_at&#39;)&quot;)
                      -&gt;get();

答案2

得分: 1

Sure, here is the translated code:

$_start_date = '2022-02-15';
$_end_date = '2022-08-20';

$users = User::where(function ($query) use ($_start_date, $_end_date) {
    $query->where(function ($query) use ($_start_date, $_end_date) {
        $query->whereRaw("start_date >= date('$_start_date')")
            ->whereRaw("end_date <= date('$_end_date')");
    })
    ->orWhere(function ($query) use ($_start_date, $_end_date) {
        $query->whereRaw("start_date <= date('$_start_date')")
            ->whereRaw("end_date >= date('$_end_date')");
    });
})->get();

This code filters the users based on the date range specified by $_start_date and $_end_date. The whereRaw method is used to construct raw SQL queries for date comparisons within the specified range.

英文:
$_start_date = &#39;2022-02-15&#39;;
$_end_date =&#39;2022-08-20&#39;;

$users = User::where(function ($query) use ($_start_date, $_end_date) {
					$query-&gt;where(function ($query) use ($_start_date, $_end_date) {
						$query-&gt;whereRaw(&quot;start_date &gt;= date(&#39;$_start_date&#39;)&quot;)
							-&gt;whereRaw(&quot;end_date &lt;= date(&#39;$_end_date&#39;)&quot;);
					})
					-&gt;orwhere(function ($query) use ($_start_date, $_end_date) {
						$query-&gt;whereRaw(&quot;start_date &lt;= date(&#39;$_start_date&#39;)&quot;)
							-&gt;whereRaw(&quot;end_date &gt;= date(&#39;$_end_date&#39;)&quot;);
					});
				})-&gt;get();

try above code this will also return between dates of start_date and end _date e.g below

$_start_date = &#39;2022-02-18&#39;;
$_end_date =&#39;2022-08-20&#39;;

and also works with below dates e.g

$_start_date = &#39;2022-02-16&#39;;
$_end_date =&#39;2022-08-18&#39;;

codes look like this in ide
使用两个日期之间的范围获取两列之间的记录,还可以使用搜索筛选条件。

答案3

得分: 0

根据你的具体需求而定

    User::whereDate('start_date', '>=', $startDate)->where('end_date', '<=', $endDate)->get();

或者

    User::whereDate('start_date', '<', $User)->whereDate('end_date', '>', $startDate)->get();
英文:

Depends on what you need exactly

User::whereDate(&#39;start_date&#39;, &#39;&gt;=&#39;, $startDate)-&gt;where(&#39;end_date&#39;, &#39;&lt;=&#39;, $endDate)-&gt;get();

or

User::whereDate(&#39;start_date&#39;, &#39;&lt;&#39;, $User)-&gt;whereDate(&#39;end_date&#39;, &#39;&gt;&#39;, $startDate)-&gt;get();

答案4

得分: 0

我认为您想要获取从start_dateend_date之间至少存在一天的记录,这些记录存在于2022年2月15日到2022年8月20日之间:

$startDate = '2022-02-15';
$endDate = '2022-08-20';
$users = User::whereDate('start_at', '<=', $endDate)
    ->whereDate('end_at', '>=', $startDate)
    ->get();
英文:

I think you want to get the records that at least one day from start_date to end_date that exists between 2022-02-15 and 2022-08-20:

$startDate = &#39;2022-02-15&#39;;
$endDate = &#39;2022-08-20&#39;;
$users = User::whereDate(&#39;start_at&#39;, &#39;&lt;=&#39;, $endDate) // where or whereDate also should work
    -&gt;whereDate(&#39;end_at&#39;, &#39;&gt;=&#39;, $startDate)
    -&gt;get();

huangapple
  • 本文由 发表于 2023年2月8日 21:57:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定